struts2 继承ActionSupport功能之一:数据验证过程例子图文教程

!!!这是基于struts2 继承ActionSupport数据验证过程图文教程,仅供参考,如有出入,敬请见谅!!!
!!!因为这是基于struts2的,所以对创建struts2不熟悉可以去看笔者的第一篇博客《struts2创建流程》

1.新建一个控制层 RegisterAction 类

struts2 继承ActionSupport功能之一:数据验证过程例子图文教程
struts2 继承ActionSupport功能之一:数据验证过程例子图文教程
这里要注意包名和类名都要有意义!!!!
当我们创建完类之后我们就可以修改代码了,下面是代码!!!

package cn.javass.hello.struts2impl.action;

import com.opensymphony.xwork2.ActionSupport;


public class HelloWorldAction extends ActionSupport {  
    private String account;  
    private String password;  
    private String submitFlag;  
    public String execute() throws Exception {  
        this.businessExecute();  
        return "toWelcome";  
    }  
    public void validate(){   
    	        if(account==null || account.trim().length()==0){  
    	            this.addFieldError("account", "账号不可以为空");  
    	        }  
    	        if(password==null || password.trim().length()==0){  
    	            this.addFieldError("password", "密码不可以为空");  
    	        }
    	        if(password!=null && !"".equals(password.trim()) && password.trim().length()<6){  
    	            this.addFieldError("password", "密码长度至少为6位");  
    	        }  
    }  

	/** 
     * 示例方法,表示可以执行业务逻辑处理的方法, 
     */  
    public void businessExecute(){  
        System.out.println("用户输入的参数为==="+"account="+account+",password="+password+",submitFlag="+submitFlag);  
    }
    public String getAccount() {
        return account;
    }
    public void setAccount(String account) {
        this.account = account;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getSubmitFlag() {
        return submitFlag;
    }
    public void setSubmitFlag(String submitFlag) {
        this.submitFlag = submitFlag;
    }  
    
}

2.新建并配置struts.xml文件

我们右击src,在新建中找到新建xml文件,并取名为struts.xml
struts2 继承ActionSupport功能之一:数据验证过程例子图文教程
!!!当新建完xml,我们就可以配置struts.xml文件了
struts2 继承ActionSupport功能之一:数据验证过程例子图文教程
这里是代码!!

<?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>  
    <constant name="struts.devMode" value="true" />        <!-- 设置了程序的运行模式 -->
    <constant name="struts.locale" value="zh_CN"/>         <!-- 设置程序运行所使用的locale -->
    <constant name="struts.i18n.encoding" value="utf-8"/>  <!-- 设置程序运行时用的编码方式 -->
    <!-- 正确设置后面两个参数,就可以解决Struts2的中文问题了。 -->
    
  
    <package name="default"  extends="struts-default">  
        <action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">  
            <result name="toWelcome">/welcome.jsp</result> 
             <result name="input">/login.jsp</result>   
        </action>  
    </package>  
    
</struts> 

3.修改jsp文件

当我们配置完struts文件之后,就可以修改jsp文件了
!!!项目里的默认jsp文件名为index.jsp,这是笔者将名字改为了login.jsp!!!
这里是代码!!!

<%@ page language="java" contentType="text/html; charset=utf-8"  pageEncoding="utf-8"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<html>  
<head>  
<meta http-equiv="Content-Type" content="text/html;utf-8">  
<title>Insert title here</title>  
<style type="text/css">
	ul,li{
	    list-style-type:none;
	    margin:0px;
	    float:left;
		}
</style>
</head>
<body>  
<form action="helloworldAction" method="post"> 
    <input type="hidden" name="submitFlag" value="login"/>  
    <div> 
        <font color=red><s:fielderror fieldName="account"/></font>
        <br/>
          账号:<input type="text" name="account">
    </div>
    <div>
        <font color=red><s:fielderror fieldName="password"/></font>
        <br/>
            密码:<input type="password" name="password">
    </div>
    <input type="submit" value="登录">  

</form>  

</body>  

</html>  

直接将代码段复制粘贴即可!!!!

5.新建jsp文件

我们为了让登录有个效果,所以我们做个提示登陆成功的页面!!
struts2 继承ActionSupport功能之一:数据验证过程例子图文教程
!!!这里笔者将新的的jsp文件名字设为welcome.jsp,代码段如下!!!

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录页面</title>
    
  </head>
  
  <body>
    登陆成功
  </body>
</html>

6.配置web.xml文件和添加需要的jar包

!!!我们在web-root文件夹下的web-inf里找到web.xml文件然后修改为如下代码
struts2 继承ActionSupport功能之一:数据验证过程例子图文教程

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  		<filter-name>struts2</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>

	
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

至于添加jar包,可以去看笔者的第一篇博客《struts2创建流程》,里面有详细教程,这里就不再重复了,敬请体谅!!!

总结
至此我们struts2 继承ActionSupport功能之一:数据验证过程例子图文教程就结束了,如有粗略,敬请见谅!!!如有模糊的地方,可以联系笔者QQ:1967893975!!!