Struts表单验证有两种方式
1.通过继承ActionSupport,重载validate方法
2.利用struts的validate框架来验证:配置ActionName-validation.xml
方式一:
1. 前台jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login blog</title>
</head>
<body bgcolor="#bbbbbb">
<div align="center">
<s:form action="login.action" method="post" theme="simple">
Msg:<s:actionmessage/><br/>
ActionError: <s:actionerror/><br/>
nameError: <s:fielderror><s:param>tuser</s:param></s:fielderror><br/>
Username:<s:textfield name="tusername"></s:textfield><br/>
passError: <s:fielderror><s:param>tpsw</s:param></s:fielderror><br/>
Password:<s:textfield name="tpassword"></s:textfield>
<s:submit></s:submit>
</s:form>
</div>
</body>
</html>
<s:actionmessage/>提示action运行状态
<s:actionerror/>提示action执行错误
<s:fielderror>提示验证错误
2.action类
主要接受前台传参tusername和tpassword
public class LoginAction extends ActionSupport{
private String tusername;
private String tpassword;
/*****************************************
* 后台给前台传值
* 1.通过Action的属性,Get方法传值
* 2.通过ActionContext---->session
* 3.通过ServletActionContext----->request response
*
* servlet是单例的,action不是。
* servlet效率更高
* Action如果是单例,属性不安全 ,所以Struts是线程安全的, 每次请求都会创建一个action来为线程服务
****************************************/
public String getTpassword() {
return tpassword;
}
public String getTusername() {
return tusername;
}
public void setTpassword(String tpassword) {
this.tpassword = tpassword;
}
public void setTusername(String tusername) {
this.tusername = tusername;
}
/**
* default function
*/
public String execute(){
return "success";
}
@Override
public void validate() {
//如果有错,默认会产生一个input的result,所以必须在xml中配置一个result为input
if(tusername==null && tusername.equals("")){
addFieldError("tuser", "cannot be null");
addActionError("user is null");
addActionMessage("error1");
}
if(tpassword==null || tpassword.equals("")){
addFieldError("tpsw", "password cannot be null");
addActionError("password is null");
addActionMessage("error2");
}
}
}
3.配置Struts.xml
<struts>
<include file="struts-default.xml"></include>
<package name="system" extends="struts-default" namespace="/">
<action name="login" class="com.scu.blog.action.LoginAction">
<result name="success">/home.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
结果如下:
Msg:
error1
error2
ActionError:
user is null
password is null
nameError:
cannot be null
passError:
password cannot be null
方法二: 使用struts validate框架
1.前台
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login blog</title>
</head>
<body bgcolor="#bbbbbb">
<div align="center">
<s:form action="login" namespace="/">
<s:textfield label="Username" name="tusername"></s:textfield><br/>
<s:textfield label="Password" name="tpassword"></s:textfield>
<s:submit></s:submit>
</s:form>
</div>
</body>
</html>
2.配置validation.xml
要验证的.class文件在同一目录下,而且配置文件名要使用如下两个规则中的一个来命名:
<ActionClassName>-validation.xml
<ActionClassName>-<ActionAliasName>-validation.xml
其中<ActionAliasName>就是struts.xml中<ation>的name属性值
本例使用的是LoginAction所以配置文件为LoginAction-validation.xml 并且放置于同一个目录下面
本例使用的是LoginAction所以配置文件为LoginAction-validation.xml 并且放置于同一个目录下面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="tusername">
<field-validator type="requiredstring">
<message>请输入姓名</message>
</field-validator>
</field>
<field name="tpassword">
<field-validator type="requiredstring">
<message>请输入密码</message>
</field-validator>
</field>
</validators>
更多配置标签可以去查手册http://struts.apache.org/2.0.11.1/docs/validation.html
3.后台代码
public class LoginAction extends ActionSupport{
private String tusername;
private String tpassword;
public String getTpassword() {
return tpassword;
}
public String getTusername() {
return tusername;
}
public void setTpassword(String tpassword) {
this.tpassword = tpassword;
}
public void setTusername(String tusername) {
this.tusername = tusername;
}
/**
* default function
*/
public String execute(){
return "success";
}
}
结果为
请输入姓名 | |
Username: | |
请输入密码 | |
Password: | |