1、理解全局和局部ActionForward的概念
2、redirect的使用
3、struts-config.xml文件不允许动态修改
4、理解动态ActionForward,动态的ActionForward是可以运行期修改的
LoginAction.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
/**
* 用户登录的Action
* @author Administrator
*
*/
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginActionForm laf = (LoginActionForm)form;
String username = laf.getUsername();
String password = laf.getPassword();
// if ("admin".equals(username) && "admin".equals(password)) {
// //转向到登录成功页面
// request.setAttribute("username", username);
// return mapping.findForward("success");
// }else {
// //转向到登录失败页面
// return mapping.findForward("error");
// }
String errorInfo = "";
try {
UserManager.getInstance().login(username, password);
//request.setAttribute("username", username);
request.getSession().setAttribute("user", username);
return mapping.findForward("success");
}catch(UserNotFoundException unfe) {
unfe.printStackTrace();
errorInfo = "用户不能找到,用户名称=[" + username + "]";
}catch(PasswordErrorException pee) {
pee.printStackTrace();
errorInfo = "密码错误";
}
request.setAttribute("errorinfo", errorInfo);
return mapping.findForward("error");
}
}
LoginActionForm.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* 登录的ActionForm,ActionForam是做数据收集的,
*
* ActionForm中的属性必须和表单中输入域的名称一致
* @author Administrator
*
*/
public class LoginActionForm extends ActionForm {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("----------LoginActionForm.reset()-----------");
}
@Override
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("----------LoginActionForm.validate()-----------");
return null;
}
}
UserManager.java
package com.bjsxt.struts;
public class UserManager {
private static UserManager instance = new UserManager();
private UserManager() {}
public static UserManager getInstance() {
return instance;
}
public void login(String username, String password) {
if (!"admin".equals(username)) {
throw new UserNotFoundException();
}
if (!"admin".equals(password)) {
throw new PasswordErrorException();
}
}
}
MustLoginAction.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class MustLoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
if (request.getSession().getAttribute("user") == null) {
ActionForward af = mapping.findForward("login");
//struts-config.xml文件不能修改
//af.setRedirect(false);
return af;
//重定向
//response.sendRedirect(request.getContextPath() + "/login.jsp");
//return null;
}
return mapping.findForward("success");
}
}
DynaActionForwardTestAction.java
package com.bjsxt.struts;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class DynaActionForwardTestAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String page = request.getParameter("page");
// ActionForward af = null;
// if ("1".equals(page)) {
// af = mapping.findForward("page1");
// }else if ("2".equals(page)) {
// af = mapping.findForward("page2");
// }
// return af;
ActionForward af = new ActionForward();
af.setPath("/page" + page + ".jsp?name=Tom");
return af;
}
}
PasswordErrorException.java
package com.bjsxt.struts;
public class PasswordErrorException extends RuntimeException {
}
UserNotFoundException.java
package com.bjsxt.struts;
public class UserNotFoundException extends RuntimeException {
}
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">
<struts-config>
<form-beans>
<form-bean name="loginForm" type="com.bjsxt.struts.LoginActionForm"/>
</form-beans>
<global-forwards>
<forward name="login" path="/login.jsp" redirect="true"/>
</global-forwards>
<action-mappings>
<action path="/login"
type="com.bjsxt.struts.LoginAction"
name="loginForm"
scope="request"
validate="false"
>
<forward name="success" path="/login_success.jsp"/>
<forward name="error" path="/login_error.jsp"/>
</action>
<action path="/mustlogin"
type="com.bjsxt.struts.MustLoginAction"
>
<!--
<forward name="login" path="/login.jsp" redirect="true"/>
-->
<forward name="success" path="/mustlogin.jsp"/>
</action>
<action path="/dynaactionforward"
type="com.bjsxt.struts.DynaActionForwardTestAction"
>
<!--
<forward name="page1" path="/page1.jsp"/>
<forward name="page2" path="/page2.jsp"/>
-->
</action>
</action-mappings>
</struts-config>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<a href="login.jsp">登录</a><br>
<a href="mustlogin.do">访问受保护的页面</a><br>
<p>
<li>动态ActionForward测试</li><br>
<form action="dynaactionforward.do" method="post">
页面:<input type="text" name="page"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
login.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>用户登录</title>
</head>
<body>
<h1>用户登录</h1>
<hr>
<form action="login.do" method="post">
用户:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
login_error.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录失败</title>
</head>
<body>
<%=request.getAttribute("errorinfo") %>
</body>
</html>
login_success.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.bjsxt.struts.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录成功</title>
</head>
<body>
<%
LoginActionForm laf = (LoginActionForm)request.getAttribute("loginForm");
%>
<%=laf.getUsername() %>,登录成功
</body>
</html>
mustlogin.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
这个是一个受保护的页面
</body>
</html>
page1.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
page1
</body>
</html>
page2.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
page2
</body>
</html>
page3.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
page3
</body>
</html>