用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>
<%@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;

}
好了,全部完成.源碼可在網盤下載.
這種方法省去了配置xxx-validtion.xml文件的麻煩,對于代碼編寫來說也比較簡單.
第一步:web.xml:





















第二步:在src下建立struts.xml:












第三步:建立兩個jsp
1.index.jsp:















2.ok.jsp:












第四步:寫出Action
LoginAction:





























































好了,全部完成.源碼可在網盤下載.
這種方法省去了配置xxx-validtion.xml文件的麻煩,對于代碼編寫來說也比較簡單.
posted on 2007-11-24 17:19 々上善若水々 閱讀(9172) 評論(2) 編輯 收藏 所屬分類: Struts2