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;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值