用Struts2中的ActionSupport中的validate方法进行表单验证

在你的的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;

}


好了,全部完成.源码可在网盘下载.

你可能感兴趣的:(用Struts2中的ActionSupport中的validate方法进行表单验证)