在上1節,我們自己寫的web框架,只能運行顯示1個HelloWorld。現在我們對其進行1次加工,讓他最少能運行1個登陸程序。
首先看login.jsp
<%@ page contentType="text/html; charset=UTF⑻" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="
java.util.*,
javax.servlet.*,
javax.servlet.http.*,
com.gc.action.User"%>
<%!public static final String _AppId = "login";%>
<%
HashMap<String,Object> infoOut=null;
if(request.getAttribute("infoOut") == null)
infoOut=new HashMap<String,Object>();
else
infoOut=(HashMap<String,Object>)request.getAttribute("infoOut");
String msg = infoOut.get("msg") == null ? "" : (String) infoOut
.get("msg");
User user = infoOut.get("user") == null ? new User()
: (User) infoOut.get("user");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>采取新的框架實現用戶登錄驗證</title>
<script language=Javascript>
function submit(target, action) {
form1.target = target;
form1.action.value = action;
form1.submit();
}
function check() {
form1.forwardJsp.value="login"; //再次跳轉回來
form1.logicName.value="LoginAction";
submit('<%="login_" + session.getId()%>','login');
}
</script>
</head>
<body leftmargin="0" topmargin="0">
<%-- 這個action 目前沒有用 我們可以隨便寫 --%>
<form name="form1" action="xx.do" method="post">
<H3>
<font color='red'><%=msg%></font>
</H3>
用戶名:<input type="text" name="username"><br> <br>
密碼: <input type="text" name="password">
<br>
<input type="button" name="button" value="提交" onClick="return check()">
<input type="reset" name="button" value="重置">
<%-- 這1次我們要從jsp端發起要求,設置3個參數 --%>
<input type="hidden" name="action" value="">
<input type="hidden" name="forwardJsp" value="">
<input type="hidden" name="logicName" value="">
</form>
<script language=Javascript>
window.name = "<%="login_"+session.getId()%>";
</script>
</body>
</html>
表現層有了,控制層我們可以復用第1節的GdServlet,現在就差模型層了。
package com.gc.action;
import java.util.HashMap;
import com.gd.action.Action;
public class LoginAction implements Action{
public HashMap<String, Object> doAction(HashMap<String, Object> infoIn) {
String action = (infoIn.get("action") == null) ? "" : (String) infoIn
.get("action");
HashMap<String, Object> infoOut = infoIn;
if (action.equals(""))
infoOut = this.doInit(infoIn);
else if (action.equals("login"))
infoOut = this.doLogin(infoIn);
return infoOut;
}
/**該方法用來實現沒有傳入動作時要處理的內容
* @param infoIn
* @return HashMap
*/
private HashMap<String, Object> doInit(HashMap<String, Object> infoIn) {
HashMap<String, Object> infoOut = infoIn;
infoOut.put("msg", "請輸入用戶名和密碼");
return infoOut;
}
/**該方法用來實現輸出HelloWorld
* @param infoIn
* @return HashMap
*/
public HashMap<String, Object> doLogin(HashMap<String, Object> infoIn){
HashMap<String, Object> infoOut = infoIn;
String username = (infoIn.get("username") == null) ? "" : (String)infoIn.get("username");
String password = (infoIn.get("password") == null) ? "" : (String)infoIn.get("password");
if ("gd".equals(username) && "123456".equals(password)) {
infoOut.put("forwardJsp", "success");
infoOut.put("msg", "登錄成功");
} else if ("gd".equals(username) && !"123456".equals(password)) {
infoOut.put("msg", "密碼毛病");
} else if (!"gd".equals(username) && "123456".equals(password)) {
infoOut.put("msg", "用戶名毛病");
} else if (!"gd".equals(username) && !"123456".equals(password)) {
infoOut.put("msg", "用戶名和密碼都輸入毛病");
} else if ("".equals(username) && "".equals(password)) {
infoOut.put("msg", "請輸入用戶名和密碼");
}
return infoOut;
}
}
infoOut.put(“forwardJsp”, “success”);
如果登陸成功,就返回success.jsp。
注意:本來forwardJsp在login.jsp里就設置了,是login。這里的邏輯是1旦成功登陸,就返回success。
success.jsp
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<%@ page import="java.sql.*,java.util.*,javax.servlet.*,
javax.servlet.http.*,java.text.*,java.math.*,com.gd.mvc.io.InfoInAndOut,com.gd.mvc.io.impl.GdInfoInAndOut"
%>
<%! public static final String _AppId = "login"; %>
<%
HashMap<String,Object> infoOut=null;
if(request.getAttribute("infoOut") == null)
infoOut=new HashMap<String,Object>();
else
infoOut=(HashMap<String,Object>)request.getAttribute("infoOut");
String msg = infoOut.get("msg") == null ? "" : (String)infoOut.get("msg");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>采取新的框架實現用戶注冊驗證</title>
</head>
<body leftmargin="0" topmargin="0">
<form name="form1" action="/myApp/do" method="post">
<H3><font color='red'><%=msg%></font><H3>
<input type="hidden" name="action" value="">
</form>
<script language=Javascript>
window.name = "<%="login_"+session.getId()%>";
</script>
</body>
</html>
我們看看效果:
我直接訪問了servlet,沒有經過jsp那就自然沒有用戶名與密碼了。
點提交以后
別的效果,我就不貼圖了,大家應當都能想出來。
在上面的基礎上,我們輸入下面的地址:
http://localhost:8700/Struts2Demo/gc/df/index.do
肯定是404notfind。
其實也很容易理解
Action action=null;
String servletPath=req.getServletPath();
String systemPath=servletPath.split("/")[1]; //systemPath 就是gc
String logicActionName=req.getParameter("logicName"); // logicActionName 就是HelloWorldAction
String actionPath=getActionPath(systemPath, logicActionName);
action=(Action) Class.forName(actionPath).newInstance();
Map<String, Object> infoOut=action.doAction(infoIn);
private String getActionPath(String systemPath,String actionName){
String actionPath="";
if (systemPath!=null)
actionPath="com."+systemPath+".action."+actionName;
return actionPath;
}
在上例中getActionPath返回的是com.gc.action.null。肯定報錯ClassNotFound。
其實我們可以把getActionPath改成以下的模樣:
private String getActionPath(String systemPath,String actionName){
String actionPath="";
if (actionName!=null) {
actionPath="com."+systemPath+".action."+actionName;
}else {
actionPath="com.gd.action.GdAction";
}
return actionPath;
}
在這個GdAction里我們放置1個默許的訪問路徑。
GdAction.java
public HashMap<String, Object> doAction(HashMap<String, Object> infoIn) {
String action = (infoIn.get("action") == null) ? "" : (String) infoIn
.get("action");
HashMap<String, Object> infoOut = new HashMap<String, Object>();
if (action.equals(""))
infoOut = this.doInit(infoIn);
return infoOut;
}
/**
* 該方法設置用戶登錄時頁面的初始信息
*
* @param infoIn
* @return HashMap
*/
private HashMap<String, Object> doInit(HashMap<String, Object> infoIn) {
HashMap<String, Object> infoOut = infoIn;
infoOut.put("forwardJsp", "../../jsp/welcome");
return infoOut;
}
OK弄定。
我們的welcom.jsp內容很簡單,就是1個歡迎頁面嘛:
<%@ page contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>歡迎您使用gf的Web框架</title>
</head>
<body center>
<H1><font color='red'>
歡迎您使用gf的Web框架
</font>
</H1>
</body>
</html>
集腋成裘,聚沙成塔。漸漸來,在下1節,我們繼續完善我們的框架。