Struts2 Action 动作
- Action 动作是 Struts 进行逻辑处理的核心,处于Controller层,Structs 的一个核心理念是屏蔽底层的 Servlet ,使用 Action 来代替 Servlet 的工作;
- 一般流程由客户端发送请求,由Action接收该请求进行逻辑处理,再有相应的 JSP 生成响应结果;
- 在 Struts1 中,所有的 Action 必须通过继承 ActionSupport 基类来实现,但是 Struts2 简便了这个过程,可以不同过继承该基类来实现一个 Action;
Action的创建
对于 Struts2 的 Action 动作类的实现,只有2个要求:
- 该 Action 类必须有一个无参数的方法返回String或结果的对象(默认该方法为 execute 方法);
- 该 Action 类必须是一个POJO(简单JavaBean对象,区别于EJB);
package demo;
public class HelloAction{
private String name;
public String execute() throws Exception {
if(name.isEmpty())
return "success";
else
return "error"
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
相应的 struts.xml 配置:
<struts>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello" class="demo.HelloAction" method="execute">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
通过以上示例可以知道,Action 类是通过 execute 方法返回相应的字符串,在 struct.xml 中对这些返回的字符串配置相应的响应页面;
Action 访问 Servlet API
在 Action 中如果要获取请求参数,或者设置设置请求参数,对 Cookie,Session进行操作,需要使用到 Servlet API ,struts2 提供了工具类
ServletActionContext 来获取相关的 Servlet API;
该类包含以下几个常用的静态方法:
static PageContext getPageContext() | 获取 Web 应用的 PageContext 对象 |
static ServletContext getServletContext() | 获取 Web 应用的 ServletContext 对象 |
static HttpServletRequest getRequest() | 获取 Web 应用的 HttpServletRequest 对象 |
static HttpServletReponse getReponse() | 获取 Web 应用的 HttpServletReponse 对象 |
以下是一些简单的示例:
public class DemoAction {
private String username;
private String password;
public String execute() throws Exception{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletReponse reponse = ServletActionContext.getReponse();
//获取表单提交参数
String username = request.getParamater("username");
String password = request.getParameter("password");
//设置请求属性
request.setAttribute("username",username);
//设置Cookie
Cookie cookie = new Cookie("user",URLEncoder.encode(username,"URF-8"));
cookie.setMaxAge(60*60);
reposne.addCookie(cookie);
}
//省略get、set方法
}
除此之外,struts 还可以通过实现接口 ServletContextAware,
ServletRequestAware, ServletReponseAware 接口
来访问 Servlet API,一般推荐上面的方法,可以很好地减少代码冗余;
Action 配置静态参数
有时候需要为一个Action类分配一些预定义或静态的参数值,可以通过以下方法:
1)在 struts.xml 中定义静态参数
使用<param>标签定义;
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="test" class="demo.TestAction">
<result name="success">/welcome.jsp</result>
<param name="EnglishParam">English</param>
<param name="ChineseParam">Chinese</param>
<param name="FranceParam">France</param>
</action>
</package>
</struts>
2)在 Action 获取静态参数
Action 类必须实现 Parameterizable 接口,由 staticParams 方法获取静态参数;
public class TestAction implements Parameterizable{
Map<String, String> params;
public void setParams(Map<String, String> params) {
this.params = params;
}
public String execute() throws Exception{
String EngishParam = params.get("EngishParam");
String ChineseParam = params.get("ChineseParam");
String FranceParam = params.get("FranceParam");
........
}
}
3)将静态参数包装为 Bean
除了直接在 Action 中获取静态参数,也可以将这些静态参数包装为一个 JavaBean,再在Action或JSP中重复调用;
public class LocaleAction implements Parameterizable{
String englishParam;
String chineseParam;
String franceParam;
public String getEnglishParam() {
return englishParam;
}
public void setEnglishParam(String englishParam) {
this.englishParam = englishParam;
}
public String getChineseParam() {
return chineseParam;
}
public void setChineseParam(String chineseParam) {
this.chineseParam = chineseParam;
}
public String getFranceParam() {
return franceParam;
}
public void setFranceParam(String franceParam) {
this.franceParam = franceParam;
}
}
Action 配置结果
Action 是通过在 struts.xml 配置 <result>标签来代替原来 Servlet 中对于视图 JSP 的 forward,除此之外,struts2还支持多种返回的结果类型技术,如 JSP、Velocity、FreeMarker,stream,redirect等,以下介绍一些常用的结果类型的配置:
1)redirect 类型——重定向到视图
<result>的默认类型是 redirect 结果类型,一般是重定向到某一个视图(如JSP,HTML等)文件;
<action name="login" class="demo.LoginAction" method="execute">
<result name="success" type="redirect">/loginSuccess.jsp</result>
</action>
2)redirectAction 类型——重定向到Action
redirectAction 类型类似于 redirect ,不过是将结果重定向到其他的一个Action类中;
<action name="login" class="demo.LoginAction" method="execute">
<result name="error" type="redirectAction">error</result>
</action>
<action name="error" class="demo.LoginEororAction" method="execute">
......
</action>
3)动态结果
struts2 支持动态结果类型,可以通过在Action name属性中使用通配符,在 class , method 中使用 {N} 表达式来实现动态结果的配置,如下:
<action name="error_*" class="demo.ErrorAction" method="execute">
<result>/WEB-INF/errorPage/{1}.jsp</result>
</action>
以上配置将所有 name 以“error_”为前缀的Action(如“error_login”"error_checking"),映射到物理名为 "demo.ErrorAction" 的Action,它们的结果转发到路径“
/WEB-INF/errorPage/”下的第1个jsp文件;
Action 的命名空间
当struts工程结果中存在多个同名的资源时,可以通过名命名结果来实现不同资源的区位划分,以避免多个模块的操作名称之间的冲突;
如一个工程中存在以下的文件结构:
test_struts
|——src
| |——demo
| | |——ErrorAction.java
| |
| |——login
| | |——ErrorAction.java
| |
| |——commit
| | |——ErrorAction.java
|
|——web
|——WEB-INF
|——pages
| |——error.jsp
|——LoginPages
| |——error.jsp
|——commitPages
|——error.jsp
在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>
<package name="default" namespace="/" extends="struts-default">
<action name="Error" class="demo.ErrorAction">
<result>/pages/error.jsp</result>
</action>
</package>
<package name="login" namespace="/login" extends="struts-default">
<action name="Error" class="login.ErrorAction">
<result>/loginPages/error.jsp</result>
</action>
</package>
<package name="commit" namespace="/commit" extends="struts-default">
<action name="Error" class="commit.ErrorAction">
<result>/commitPages/error.jsp</result>
</action>
</package>
</struts>
在URL访问以上3个"ErrorAction"的请求如下:
http://localhost:8080/test_struts/Error
http://localhost:8080/test_struts/login/Error
http://localhost:8080/test_struts/commit/Error
在URL访问以上3个"error.jsp"的请求依次如下:
http://localhost:8080/test_struts/error.jsp
http://localhost:8080/test_struts/login/error.jsp
http://localhost:8080/test_struts/commit/error.jsp