struts2结果集校验

工程网络


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Struts02</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
  	<filter-name>struts</filter-name>
  	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>
</web-app>
struts.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<constant name="devMode" value="true"></constant>
	<package name="struts" extends="struts-default" namespace="/">
	</package>
</struts>
TestAction

package com.hellojava.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import com.hellojava.entity.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.Validations;

@Action(results={@Result(location="/index.jsp",name="success"),@Result(location="/login.jsp",name="input")},value="test")
public class TestAction extends ActionSupport{
	private User user=new User();
	
	//如果一个action使用了validate() 方法做校验,那么当前的Action一定要配置一个视图(input)
//	public void validate() {
//		if(this.userName==null || "".equals(this.getUserName())){
//			this.addFieldError("userName", "用户名不能为空");
//		}
//		
//		if(this.userName!=null && (this.userName.length()<3 || this.userName.length()>10)){
//			this.addFieldError("userName", "用户名必须要3--10之间");
//		}
//		
//		if(this.userPwd==null || "".equals(this.getUserPwd())){
//			this.addFieldError("userPwd", "密码不能为空");
//		}
//	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String execute() throws Exception {
//		//在ActionSupport中定义了5个默认的视图
//		this.ERROR;"error"
//		this.INPUT;"input"
//		this.NONE;"none"
//		this.LOGIN;"login"
//		return this.SUCCESS;"success"
		
		return this.SUCCESS;
	}
}
TestAction-validation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
  		"-//Apache Struts//XWork Validator 1.0.3//EN"
  		"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
  		
<validators>
	<!-- 验证的字段 -->
	<field name="user.userName">
		<!-- 采用的验证器 -->
		<field-validator type="requiredstring">
			<message>用户名不能为空(validation xml 校验)</message>
		</field-validator>
		<field-validator type="stringlength">
			<param name="trim">true</param>
			<param name="maxLength">10</param>
			<param name="minLength">3</param>
			<message>用户名必须在${minLength}--${maxLength}之间</message>
		</field-validator>
	</field>	
	
	<field name="user.userPwd">
		<field-validator type="requiredstring">
			<message>密码不能为空(validation xml 校验)</message>
		</field-validator>
	</field>
	
	<field name="user.userAge">
		<field-validator type="required">
			<message>年龄不能为空</message>
		</field-validator>
		<field-validator type="int">
			<param name="min">18</param>
			<param name="max">120</param>
			<message>年龄必须在${min}--${max}之间</message>
		</field-validator>
	</field>
	
	<field name="user.userEmail">
		<field-validator type="requiredstring">
			<message>email 不能为空</message>
		</field-validator>
		<field-validator type="email">
			<message>email 不合法</message>
		</field-validator>
	</field>
</validators>
login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="test.action" method="post">
		用户名:<input type="text" name="user.userName"/><s:fielderror fieldName="user.userName"/><br/>
		密码:<input type="password" name="user.userPwd"/><s:fielderror fieldName="user.userPwd"/><br/>
		年龄:<input type="text" name="user.userAge"/><s:fielderror fieldName="user.userAge"/><br/>
		邮箱:<input type="text" name="user.userEmail"/><s:fielderror fieldName="user.userEmail"/><br/>
		<input type="submit" value="提交"/>
	</form>
</body>
</html>
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>success</h1>
</body>
</html>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值