1、User类:
- @SuppressWarnings("serial")
- public class User implements Serializable {
-
- private String username;
- private String password;
- private int[] specialities;
-
- 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;
- }
-
- public int[] getSpecialities() {
- return specialities;
- }
-
- public void setSpecialities(int[] specialities) {
- this.specialities = specialities;
- }
-
- }
2、Speciality类:
- public class Speciality {
-
- private int id;
- private String name;
-
- public Speciality() {
- }
-
- public Speciality(int id, String name) {
- this.id = id;
- this.name = name;
- }
-
- public int getId() {
- return id;
- }
-
- public void setId(int id) {
- this.id = id;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- }
3、UserForm类:
- public class UserForm extends ActionForm {
-
- private static final long serialVersionUID = 1L;
- private User user = new User(); // 必须初始化
- private String password2;
-
- public String getPassword2() {
- return password2;
- }
-
- public void setPassword2(String password2) {
- this.password2 = password2;
- }
-
- public User getUser() {
- return user;
- }
-
- public void setUser(User user) {
- this.user = user;
- }
-
- @Override
- public ActionErrors validate(ActionMapping mapping,
- HttpServletRequest request) {
- ActionErrors errors = new ActionErrors();
- String name = user.getUsername();
- if (null == name || "".equals(name.trim())) {
- errors.add("username", new ActionMessage("error.username"));
- }
- if (!password2.equals(user.getPassword())) {
- errors.add("password", new ActionMessage("error.password"));
- }
- return errors;
- }
-
- }
4、IndexAction类:
- public class IndexAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- saveToken(request); // 保存sessionId
- request.setAttribute("specialities", getSpecialities());
- return mapping.findForward("index");
- }
-
- private Speciality[] getSpecialities() {
- return new Speciality[] { new Speciality(1, "footbal"),
- new Speciality(2, "basketball"),
- new Speciality(3, "volleyball"),
- new Speciality(4, "ping-pong"), new Speciality(5, "baseball"), };
- }
- }
5、LoginAction类:
- public class LoginAction extends Action {
-
- @Override
- public ActionForward execute(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- UserForm userForm = (UserForm) form;
- String username = userForm.getUser().getUsername();
- ActionMessages errors = new ActionMessages();
- if (null != username && !"".equals(username.trim())
- && "wgy".equals(username)) {
- errors.add("username", new ActionMessage("error.repeated"));
- saveErrors(request, errors);
- return mapping.getInputForward();
- }
- return mapping.findForward("success");
- }
-
- }
6、struts-config.xml配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts-config PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
- "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
- <struts-config>
- <form-beans>
- <form-bean name="userForm" type="com.wgy.struts1.UserForm" />
- </form-beans>
- <action-mappings>
- <action path="/login" type="com.wgy.struts1.LoginAction" name="userForm"
- input="/index.do"> <!-- 这里index后必须带.do -->
- <forward name="success" path="/success.jsp"></forward>
- </action>
- <action path="/index" type="com.wgy.struts1.IndexAction" name="userForm"
- validate="false">
- <forward name="index" path="/index.jsp"></forward>
- </action>
- </action-mappings>
- <message-resources parameter="com.wgy.resources.Application" />
- </struts-config>
7、Application.proprties文件(位于com.wgy.resources下):
- error.username=<h6><font color="red">\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\uFF01</font></h6>
- error.password=<h6><font color="red">\u4E24\u6B21\u5BC6\u7801\u5FC5\u987B\u4E00\u81F4\uFF01</font></h6>
- error.repeated=<h6><font color="red">\u7528\u6237\u540D\u91CD\u590D\uFF01</font></h6>
8、index.jsp页面:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
- <!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=UTF-8">
- <title>index.jsp</title>
- </head>
- <body>
- <form action="login.do" method="post">
- username:<input type="text" name="user.username"
- value="${userForm.user.username }" />
- <html:errors property="username" />
- <br />
- <!-- 这里User对象保存在session范围,需将其序列化 -->
- passwrod:<input type="text" name="user.password"
- value="${userForm.user.password }" /> <br />
- passwrod2:<input type="text" name="password2"
- value="${userForm.password2 }" />
- <html:errors property="password" />
- <br />
- <%-- 爱好:<input type="checkbox" name="user.specialities" value="1" />football
- <input type="checkbox" name="user.specialities" value="2" />basketball
- <input type="checkbox" name="user.specialities" value="3" />volleyball
- <input type="checkbox" name="user.specialities" value="4" />ping-pong
- <input type="checkbox" name="user.specialities" value="5" />baseball <br /> --%>
- 爱好:
- <c:forEach items="${specialities }" var="speciality">
- <!-- 校验失败时,已选中的复选框仍被选中 -->
- <c:remove var="checked"/>
- <c:forEach items="${userForm.user.specialities }" var="hasSpeciality">
- <c:if test="${speciality.id==hasSpeciality }">
- <c:set var="checked" value='checked="checked"' />
- </c:if>
- </c:forEach>
- <input type="checkbox" name="user.specialities"
- value="${speciality.id }" ${checked } />${speciality.name }
- </c:forEach>
- <br /> <input type="submit" value="登录" />
- <hr />
- <c:forEach items="${sessionScope }" var="oneAttr">
- ${oneAttr.key } --> ${oneAttr.value } <br />
- </c:forEach>
- </form>
- </body>
- </html>
9、success.jsp页面:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!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=UTF-8">
- <title>success.jsp</title>
- </head>
- <body>
- ${userForm.user.username } <br />
- <c:forEach items="${userForm.user.specialities }" var="speciality">
- ${speciality }
- </c:forEach>
- <hr />
- <c:forEach items="${requestScope }" var="oneAttr">
- ${oneAttr.key } --> ${oneAttr.value } <br />
- </c:forEach>
- </body>
- </html>
小结:
1) 本例中JSTL表达式迭代的应用;
2) struts-config.xml文件中action的attribute属性:为web域中FormBean起的别名,默认值和name属性的值一致;
3) action的scope属性:默认值为session;
4) action的input属性:表单校验失败时,跳转的地址;
5) <html:errors>标签使用时,需配置资源文件,由此引出struts1的国际化。