在Struts2中,Action可以不实现任何特殊的接口或者继承特殊的类,仅仅是一个POJO(Plain Old Java Object,简单的Java对象)就可以;也可以实现Xwork2中的Action接口;但是由于Xwork的Action接口非常简单,为程序员提供的帮助有限,因此,在实际开发中,会更多的使用继承ActionSupport类来实现Action的方式,如下所示:
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorldAction extends ActionSupport {
//省略
}
基本的数据验证
新建控制层HelloWorldAction.Java文件
- 要实现数据验证的功能,只需要在Action类中覆盖实现validate方法即可;在validate方法内部,对请求传递过来的数据进行校验,如果不满足要求,那么添加例外信息到父类用于存放例外的集合中。示例代码如下:
package com.hnpi.actionsupport;
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;
}
}
- 从上面的示例可以看出,在validate方法中,可以对用户请求中传递过来的数据进行验证,同一个数据可以进行多方面的验证。
如果验证结果是数据不正确,那么就使用父类提供的addFieldError方法来添加验证的错误消息。addFieldError方法有两个参数,前面的是消息的key值,后面是具体的消息。
strurs.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>
<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="helloworld" extends="struts-default">
<action name="helloworldAction" class="com.hnpi.actionsupport.HelloWorldAction">
<result name="toWelcome">/welcome.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
配置项目所需的web.xml文件
<?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">
<welcome-file-list>
<welcome-file>welcome.jsp</welcome-file>
</welcome-file-list>
<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>
</web-app>
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>
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>