validate()方法验证
一个ACTION类继承ActionSupport有何用处?
答:在ActionSupport中实现了很多接口,就拿一个来讲,validate()方法,是默认的验证。我们可以在我们的action类中重写一下此方法。比方说验证用户名是否为空。这个方法不需要调用,当S:FORM表单提交了,到了action类中会首先执行此方法。对数据进行验证。
(1)、前台页面
<s:form action="login" method="post">
<s:textfeild name="username" label="username"/>
</s:form>
(2)、login.action页面
class Login extends ActionSupport{
private String username;
...//username的set(),get()方法
//execute()方法
public String validate(){
if("".equals(this.getUsername()) || null == this.getUsername()){
this.addFieldErro("username","username can't null");//参数1是前台组件名称,后面是提示。此错误信息会在此组件的前面显示这句话
}
}
}
(3)、struts.xml文件
输出的错误信息会返回一个input 值。所以XML文件中需要定义一个结果为 input 的结果
<result name="input">/...jsp</result>
例:
在你的的web中加入struts2的jar包支持.
第一步:web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第二步:在src下建立struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="com" extends="struts-default">
<action name="login" class="com.LoginAction">
<result name="input">/index.jsp</result>
<result name="success">/ok.jsp</result>
</action>
</package>
</struts>
第三步:建立两个jsp
1.index.jsp:
<%@page language="java" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<s:form action="login" method="post">
<s:textfield name="username" label="User Name:"/><br />
<s:password name="password" label="Password:"/><br />
<s:submit value="Submit" />
</s:form>
</body>
</html>
2.ok.jsp:
<%@page language="java" pageEncoding="GB18030"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>ok</title>
</head>
<body>
UserName:<s:property value="username"/>
Password:<s:property value="password"/>
</body>
</html>
第四步:写出Action
LoginAction:
package com;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport
{
private String username;
private String password;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
@Override
public String execute() throws Exception
{
if("admin".equals(this.getUsername().trim()) && "admin".equals(this.getPassword().trim()))
{
return SUCCESS;
}
else
{
this.addFieldError("username", "UserName or password is wrong!");
return INPUT;
}
}
@Override
public void validate()
{
if(null == this.getUsername() || "".equals(this.getUsername().trim()))
{
//第一个参数表示表单中的textfield的name,第二参数是提示信息
this.addFieldError("username", "UserName is required!");
}
if(null == this.getPassword() || "".equals(this.getPassword().trim()))
{
this.addFieldError("password", "Password is required!");
}
}
private static final long serialVersionUID = 4771028725069625041L;
}