struts1校驗和JSTL表達式的迭代

1、User類:

  1. @SuppressWarnings("serial")  
  2. public class User implements Serializable {  
  3.  
  4.     private String username;  
  5.     private String password;  
  6.     private int[] specialities;  
  7.  
  8.     public String getUsername() {  
  9.         return username;  
  10.     }  
  11.  
  12.     public void setUsername(String username) {  
  13.         this.username = username;  
  14.     }  
  15.  
  16.     public String getPassword() {  
  17.         return password;  
  18.     }  
  19.  
  20.     public void setPassword(String password) {  
  21.         this.password = password;  
  22.     }  
  23.  
  24.     public int[] getSpecialities() {  
  25.         return specialities;  
  26.     }  
  27.  
  28.     public void setSpecialities(int[] specialities) {  
  29.         this.specialities = specialities;  
  30.     }  
  31.  

2、Speciality類:

  1. public class Speciality {  
  2.  
  3.     private int id;  
  4.     private String name;  
  5.  
  6.     public Speciality() {  
  7.     }  
  8.  
  9.     public Speciality(int id, String name) {  
  10.         this.id = id;  
  11.         this.name = name;  
  12.     }  
  13.  
  14.     public int getId() {  
  15.         return id;  
  16.     }  
  17.  
  18.     public void setId(int id) {  
  19.         this.id = id;  
  20.     }  
  21.  
  22.     public String getName() {  
  23.         return name;  
  24.     }  
  25.  
  26.     public void setName(String name) {  
  27.         this.name = name;  
  28.     }  

3、UserForm類:

  1. public class UserForm extends ActionForm {  
  2.  
  3.     private static final long serialVersionUID = 1L;  
  4.     private User user = new User(); // 必須初始化  
  5.     private String password2;  
  6.  
  7.     public String getPassword2() {  
  8.         return password2;  
  9.     }  
  10.  
  11.     public void setPassword2(String password2) {  
  12.         this.password2 = password2;  
  13.     }  
  14.  
  15.     public User getUser() {  
  16.         return user;  
  17.     }  
  18.  
  19.     public void setUser(User user) {  
  20.         this.user = user;  
  21.     }  
  22.  
  23.     @Override 
  24.     public ActionErrors validate(ActionMapping mapping,  
  25.             HttpServletRequest request) {  
  26.         ActionErrors errors = new ActionErrors();  
  27.         String name = user.getUsername();  
  28.         if (null == name || "".equals(name.trim())) {  
  29.             errors.add("username"new ActionMessage("error.username"));  
  30.         }  
  31.         if (!password2.equals(user.getPassword())) {  
  32.             errors.add("password"new ActionMessage("error.password"));  
  33.         }  
  34.         return errors;  
  35.     }  
  36.  

4、IndexAction類:

  1. public class IndexAction extends Action {  
  2.  
  3.     @Override 
  4.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  5.             HttpServletRequest request, HttpServletResponse response)  
  6.             throws Exception {  
  7.         saveToken(request); // 保存sessionId  
  8.         request.setAttribute("specialities", getSpecialities());  
  9.         return mapping.findForward("index");  
  10.     }  
  11.  
  12.     private Speciality[] getSpecialities() {  
  13.         return new Speciality[] { new Speciality(1"footbal"),  
  14.                 new Speciality(2"basketball"),  
  15.                 new Speciality(3"volleyball"),  
  16.                 new Speciality(4"ping-pong"), new Speciality(5"baseball"), };  
  17.     }  

5、LoginAction類:

  1. public class LoginAction extends Action {  
  2.  
  3.     @Override 
  4.     public ActionForward execute(ActionMapping mapping, ActionForm form,  
  5.             HttpServletRequest request, HttpServletResponse response)  
  6.             throws Exception {  
  7.         UserForm userForm = (UserForm) form;  
  8.         String username = userForm.getUser().getUsername();  
  9.         ActionMessages errors = new ActionMessages();  
  10.         if (null != username && !"".equals(username.trim())  
  11.                 && "wgy".equals(username)) {  
  12.             errors.add("username"new ActionMessage("error.repeated"));  
  13.             saveErrors(request, errors);  
  14.             return mapping.getInputForward();  
  15.         }  
  16.         return mapping.findForward("success");  
  17.     }  
  18.  

6、struts-config.xml配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!DOCTYPE struts-config PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"  
  4.     "http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd"> 
  5. <struts-config> 
  6.     <form-beans> 
  7.         <form-bean name="userForm" type="com.wgy.struts1.UserForm" /> 
  8.     </form-beans> 
  9.     <action-mappings> 
  10.         <action path="/login" type="com.wgy.struts1.LoginAction" name="userForm" 
  11.             input="/index.do"> <!-- 這裏index後必須帶.do --> 
  12.             <forward name="success" path="/success.jsp"></forward> 
  13.         </action> 
  14.         <action path="/index" type="com.wgy.struts1.IndexAction" name="userForm" 
  15.             validate="false"> 
  16.             <forward name="index" path="/index.jsp"></forward> 
  17.         </action> 
  18.     </action-mappings> 
  19.     <message-resources parameter="com.wgy.resources.Application" /> 
  20. </struts-config> 

7、Application.proprties文件(位於com.wgy.resources下):

  1. error.username=<h6><font color="red">\u7528\u6237\u540D\u4E0D\u80FD\u4E3A\u7A7A\uFF01</font></h6> 
  2. error.password=<h6><font color="red">\u4E24\u6B21\u5BC6\u7801\u5FC5\u987B\u4E00\u81F4\uFF01</font></h6> 
  3. error.repeated=<h6><font color="red">\u7528\u6237\u540D\u91CD\u590D\uFF01</font></h6> 

8、index.jsp頁面:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> 
  4. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  6. <html> 
  7. <head> 
  8. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  9. <title>index.jsp</title> 
  10. </head> 
  11. <body> 
  12.     <form action="login.do" method="post"> 
  13.         username:<input type="text" name="user.username" 
  14.             value="${userForm.user.username }" /> 
  15.         <html:errors property="username" /> 
  16.         <br /> 
  17.         <!-- 這裏User對象保存在session範圍,需將其序列化 --> 
  18.         passwrod:<input type="text" name="user.password" 
  19.             value="${userForm.user.password }" /> <br />   
  20.         passwrod2:<input type="text" name="password2"   
  21.             value="${userForm.password2 }" /> 
  22.         <html:errors property="password" /> 
  23.         <br /> 
  24.         <%-- 愛好:<input type="checkbox" name="user.specialities" value="1" />football  
  25.         <input type="checkbox" name="user.specialities" value="2" />basketball  
  26.         <input type="checkbox" name="user.specialities" value="3" />volleyball  
  27.         <input type="checkbox" name="user.specialities" value="4" />ping-pong  
  28.         <input type="checkbox" name="user.specialities" value="5" />baseball <br /> --%> 
  29.         愛好:  
  30.         <c:forEach items="${specialities }" var="speciality"> 
  31.             <!-- 校驗失敗時,已選中的複選框仍被選中 --> 
  32.             <c:remove var="checked"/> 
  33.             <c:forEach items="${userForm.user.specialities }" var="hasSpeciality"> 
  34.                 <c:if test="${speciality.id==hasSpeciality }"> 
  35.                     <c:set var="checked" value='checked="checked"' /> 
  36.                 </c:if> 
  37.             </c:forEach> 
  38.             <input type="checkbox" name="user.specialities" 
  39.                 value="${speciality.id }" ${checked } />${speciality.name }  
  40.         </c:forEach> 
  41.         <br /> <input type="submit" value="登錄" /> 
  42.         <hr /> 
  43.         <c:forEach items="${sessionScope }" var="oneAttr"> 
  44.             ${oneAttr.key } --> ${oneAttr.value } <br /> 
  45.         </c:forEach> 
  46.     </form> 
  47. </body> 
  48. </html> 

9、success.jsp頁面:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" 
  2.     pageEncoding="UTF-8"%> 
  3. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  5. <html> 
  6. <head> 
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  8. <title>success.jsp</title> 
  9. </head> 
  10. <body> 
  11.     ${userForm.user.username } <br /> 
  12.     <c:forEach items="${userForm.user.specialities }" var="speciality"> 
  13.         ${speciality }  
  14.     </c:forEach> 
  15.     <hr /> 
  16.     <c:forEach items="${requestScope }" var="oneAttr"> 
  17.         ${oneAttr.key } --> ${oneAttr.value } <br /> 
  18.     </c:forEach> 
  19. </body> 
  20. </html> 

小結:

1) 本例中JSTL表達式迭代的應用;

2) struts-config.xml文件中action的attribute屬性:爲web域中FormBean起的別名,默認值和name屬性的值一致;

3) action的scope屬性:默認值爲session;

4) action的input屬性:表單校驗失敗時,跳轉的地址;

5) <html:errors>標籤使用時,需配置資源文件,由此引出struts1的國際化。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章