今天学习了struts2的标签的表单验证
第一步:JSP页面
第二步:action
首先action继承ActionSupport
第三步:struts.xml
第一步:JSP页面
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="login">
<s:textfield name="username" label="username"></s:textfield>
<s:textfield name="password" label="password"></s:textfield>
<s:submit label="submit"></s:submit>
</s:form>
第二步:action
首先action继承ActionSupport
package com.test.action;
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;
}
public String execute() throws Exception
{
return "success";
}
@Override
public void validate() {
if(null==this.getUsername()||"".equals(this.getUsername().trim()))
{
this.addFieldError("username", "username required");
}
if(null==this.getPassword()||"".equals(this.getPassword().trim()))
{
this.addFieldError("password"," password required");
}
}
}
第三步: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="struts2" extends="struts-default">
<action name="login" class="com.test.action.LoginAction">
<result name="input">/login2.jsp</result>
<result name="success">/result.jsp</result>
</action>
</package>
</struts>