`
shoppingbill
  • 浏览: 60672 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

struts2 annotation validation simple

阅读更多
Required jar:

commons-fileupload-1.2.X.jar
commons-logging-1.0.X.jar
freemarker-2.3.X.jar
ognl-2.1.X.jar
struts2-convention-plugin-2.1.X.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
	<display-name>Struts2Tutorial</display-name>
	<filter>
	  <filter-name>struts2</filter-name>
	  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
	  <init-param>
	     <param-name>actionPackages</param-name>
	     <param-value> com.qtec.struts2</param-value>
	  </init-param>
	</filter>
	<filter-mapping>
	   <filter-name>struts2</filter-name>
	   <url-pattern>/*</url-pattern>
	</filter-mapping>
	<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>
</web-app>

index.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>Index Jsp</title>
</head>
<body>
<s:form action="helloWorld" theme="simple">
<s:textfield name="username"/><s:fielderror fieldName="username"/>
<s:submit value="Greetting"/>
</s:form>
</body>
</html>

greetting.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>Index Jsp</title>
</head>
<body>
<h3>Hello,</h3> 
<s:property value="username"/>
</body>
</html>

HelloWorld.java
package com.qtec.struts2.day1;

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

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
/**
 * 
 * @author Bill.Zhang
 *
 */
@ParentPackage("struts-default")
@Namespace("/")
@Validations
public class HelloWorldAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Action(value="helloWorld",
			results={
			@Result(name="success",location="/greetting.jsp"),
			@Result(name="input",location="/index.jsp")
	}
	)
/*	@Validations(
			requiredStrings={@RequiredStringValidator(message="Supply username",fieldName="username")}
	)*/
	public String execute() throws Exception {
		return SUCCESS;
	} 
	
	private String username;
	public String getUsername() {
		return username;
	}
	@RequiredStringValidator(message="Supply username",fieldName="username")
	@StringLengthFieldValidator(minLength="2",maxLength="12",message="username minLength is ${minLength}, maxLength is ${maxLength}")
	public void setUsername(String username) {
		this.username = username;
	}
}

分享到:
评论
1 楼 herowzz 2009-09-22  
annotation validation 越看越丑陋...

相关推荐

Global site tag (gtag.js) - Google Analytics