[转帖]struts1校验和JSTL表达式的迭代_Tomcat, WebLogic及J2EE讨论区_Weblogic技术|Tuxedo技术|中间件技术|Oracle论坛|JAVA论坛|Linux/Unix技术|hadoop论坛_联动北方技术论坛  
网站首页 | 关于我们 | 服务中心 | 经验交流 | 公司荣誉 | 成功案例 | 合作伙伴 | 联系我们 |
联动北方-国内领先的云技术服务提供商
»  游客             当前位置:  论坛首页 »  自由讨论区 »  Tomcat, WebLogic及J2EE讨论区 »
总帖数
1
每页帖数
101/1页1
返回列表
0
发起投票  发起投票 发新帖子
查看: 3500 | 回复: 0   主题: [转帖]struts1校验和JSTL表达式的迭代        下一篇 
Leon
注册用户
等级:少校
经验:1436
发帖:116
精华:7
注册:2013-1-4
状态:离线
发送短消息息给Leon 加好友    发送短消息息给Leon 发消息
发表于: IP:您无权察看 2013-1-8 12:56:55 | [全部帖] [楼主帖] 楼主

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的国际化。




赞(0)    操作        顶端 
总帖数
1
每页帖数
101/1页1
返回列表
发新帖子
请输入验证码: 点击刷新验证码
您需要登录后才可以回帖 登录 | 注册
技术讨论