为了方便实现Action,大多数情况下都会继承com.opensymphony.xwork2.ActionSupport类, 并重载(Override)此类里的String execute()方法,因为ActionSupport已经实现了Action接口, 还实现了Validateable接口,提供了数据校验功能。通过继承该ActionSupport类,可以简化Struts 2的Action开发。
1. 必须有一个完整的struts2框架
2. 建立视图层
2.1 登录页面(index.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>登录页面</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>
2.2 登录成功后的欢迎页面 (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>
3. 建立控制层的ActionSupport (HelloWorldAction.java)
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;
}
}
4. 在src下建立struts.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"/> <!-- 设置程序运行时用的编码方式 -->
<package name="helloworld" extends="struts-default">
<action name="helloworldAction" class="cn.javass.hello.struts2impl.action.HelloWorldAction">
<!-- 定义welcome的逻辑视图名,对应welcome.jsp页面 -->
<result name="toWelcome">/welcome.jsp</result>
<!-- 定义input的逻辑视图名,对应login.jsp页面 -->
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
5. 在web.xml中配置struts2的过滤器 (wed.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>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- 过滤器用来初始化struts2并处理所有的wed请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
代码效果图: