Struts.xml
<action name="login" class="com.cug.demo04.LoginAction"> <result>/demo04/success.jsp</result> <result name="input">/demo04/login.jsp</result> </action> <action name="login1" class="com.cug.demo04.LoginAction" method="login"> <result>/demo04/success.jsp</result> <result name="input">/demo04/login.jsp</result> </action>login.jsp
<body> <s:fielderror></s:fielderror> <form action="${pageContext.request.contextPath}/login" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value="登陆"/> </form> <hr/> <form action="${pageContext.request.contextPath}/login1" method="post"> 用户名:<input type="text" name="username"><br/> 密码:<input type="password" name="password"><br/> <input type="submit" value="登陆"/> </form> </body>
package com.cug.demo04; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport{ private String username; private String password; public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } @Override public String execute() throws Exception { return SUCCESS; } @Override public void validate() { System.out.println("validate~"); if(username==null || username.trim().isEmpty()){ this.addFieldError("username", "用户名不能为空"); } if(password==null || password.trim().isEmpty()){ this.addFieldError("password", "密码不能为空"); }else{ if(password.length()<5 || password.length()>8){ this.addFieldError("password", "密码的长度应该在5~8之间"); } } } public String login(){ return SUCCESS; } public void validateLogin(){ System.out.println("validateLogin~"); } }
JavaEE----使用Struts2的输入校验之手动编码校验
原文地址:http://blog.csdn.net/bluzelee2011/article/details/43056821