码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2重写validateXXX方法校验多个处理逻辑

时间:2015-05-12 11:25:06      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:重写validatexxx校验

       在struts2表单校验中,我们习惯些一个Action对应一个表单,其实可以写多个表单对应一个Action,这样可以减少类的数量,具体实现的思想是在一个Action中有多个方法,而每一个方法对应的是每一个Action。下面的例子中有两个表单页面reg.jsp和login.jsp,这两个表单的验证逻辑是由OneAction的reg和login方法完成的。

package action;


import com.opensymphony.xwork2.ActionSupport;
public class OneAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String user;
private String pass;
private String name;
private int age;
private String pass1;
private String pass2;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPass1() {
return pass1;
}
public void setPass1(String pass1) {
this.pass1 = pass1;
}
public String getPass2() {
return pass2;
}
public void setPass2(String pass2) {
this.pass2 = pass2;
}
@Override
public String execute() throws Exception {
return super.execute();
}
public String login() {
return SUCCESS;
}
public void validateLogin() {
if (!"admin".equals(getUser()) && !"admin".equals(getPass())) {
this.addFieldError("user", "用户名或密码不对!");
}
}
public String reg() {
return SUCCESS;
}
public void validateReg() {
if (getName() == null || "".equals(getName())) {
this.addFieldError("name", "用户名不能为空!");
}
if (getAge() > 130 || getAge() < 0) {
this.addFieldError("age", "年龄有问题!");
}
if (getPass1() == null || "".equals(getPass1())
|| !getPass1().equals(getPass2())) {
this.addFieldError("pass1", "密码有问题!");
}
}
}

其中,上面的validateLogin()和validateReg()分别对login和reg表单的校验,validateLogin方法和login方法时对应
的。通过validateXXX方法可以对局部数据惊醒校验,当校验失败是Struts2会自动跳转到input。
其中reg方法用于处理reg.jsp视图,login方法用于处理login.jsp视图,struts.xml配置如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="oneaction" extends="struts-default">
<action name="reg" class="action.OneAction" method="reg">
<result name="success">/success.jsp</result>
<result name="input">/reg.jsp</result>
</action>
<action name="login" class="action.OneAction" method="login">
<result name="success">/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>    


login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录页面</title>
  </head>
  <body>
  <s:form action="login">
  <s:textfield name="user" label="用户名"></s:textfield>
  <s:password name="pass" label="密码框"></s:password>
  <s:submit value="登录"></s:submit>
  </s:form>
  </body>
</html>

reg.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册页面</title>
</head>
<body>
<s:form action="reg">
<s:textfield name="name" label="用户名"></s:textfield>
<s:textfield name="age" label="年龄"></s:textfield>
<s:password name="pass1" label="密码框"></s:password>
<s:password name="pass2" label="重复密码"></s:password>
<s:submit value="注册"></s:submit>
</s:form>
</body>
</html>

Struts2重写validateXXX方法校验多个处理逻辑

标签:重写validatexxx校验

原文地址:http://blog.csdn.net/qq_20545159/article/details/45666345

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!