1、struts-config.xml文件中,每个<action>标签对应一个ActionMapping实例
2、了解<action>标签中的forward和unknown属性的含义
3、了解采用jstl和struts标签保持页面数据
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>
<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"/>
-->
<forward name="error" path="/login.jsp"/>
</action>
<action path="/login2"
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"/>
-->
<forward name="error" path="/login_struts_tag.jsp"/>
</action>
<action path="/login1"
forward="/login.jsp"
></action>
<action path="/testunknown"
unknown="true"
forward="/testunknown.jsp"
></action>
<action path="/loginstrutstag"
forward="/login_struts_tag.jsp"
></action>
</action-mappings>
</struts-config>
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);
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();
}
}
}
PasswordErrorException.java
package com.bjsxt.struts;
public class PasswordErrorException extends RuntimeException {
}
UserNotFoundException.java
package com.bjsxt.struts;
public class UserNotFoundException extends RuntimeException {
}
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>
<h1>测试ActionMapping</h1>
<a href="login1.do">登录(采用jstl)</a><br>
<a href="loginstrutstag.do">登录(采用struts tag)</a>
</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" value="${loginForm.username }"><br>
密码:<input type="password" name="password" value="${loginForm.password }"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
login_struts_tag.jsp
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html"%>
<!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>用户登录(采用struts标签)</h1>
<hr>
<html:form action="login2.do" method="post">
用户:<html:text property="username"/><br>
密码:<html:password property="password"/><br>
<html:submit value="登录"/>
</html:form>
</body>
</html>
test_unknown.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>
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>
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>