struts2总结
来源:互联网 发布:淘宝关键词工具 编辑:程序博客网 时间:2024/04/27 22:03
1:环境搭建(helloworld)
1: 下载struts2 包(2.1.8.1):
struts-2.1.8.1-all:下面有四个子包apps,docs,lib,src.
2: struts2jar包:
从struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\lib中复制所有jar文件,它是最小的jar文件集合。
3:struts2参考手册的使用:
struts-2.1.8.1-all/struts-2.1.8.1/docs/docs/guides.html
4:修改web.xml:
打开struts-2.1.8.1-all/struts-2.1.8.1/docs/docs/webxml.html文件,把如下内容写入web.xml.
<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>5:建立struts.xml文件从struts-2.1.8.1-all\struts-2.1.8.1\apps\struts2-blank-2.1.8.1\WEB-INF\classes把struts.xml文件复制到项目的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> <!-- Add packages here --> <constant name="struts.devMode" value="true" /> <package name="default" namespace="/" extends="struts-default"> <action name="hello"> <result> /hello.jsp </result> </action> </package></struts>6:在webroot下新建一个hello.jsp文件。7:项目发布,在地址栏输入http://localhost:端口名/项目名/hello.action,就可以看到效果了。
2:Struts2 Action(一)
什么是Struts2 Action呢?
实现了
public String execute() throws Exception;
的任何一个java类都可以被认为是Struts2 Action。
注意点:
1:方法名默认为execute,也可以自定义方法名。
2:返回值应该为:success,none,error,input和login;
Struts2 Action的实现方法?(三种)
1:
public class IndexAction1 {
public String execute(){
return "success";
}
}
2:
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action{
public String execute(){
return SUCCESS;
}
}
3:
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport {
private static final long serialVersionUID = 1L;
@Override
public String execute(){
return ERROR;
}
}
请参考:com.opensymphony.xwork2.Action接口,com.opensymphony.xwork2.ActionSupport类。
Action接口定义了Struts2 Action应该实现的方法和5个字符串常量作为方法的返回值。
ActionSupport类是Struts2提供的一个具有基本实现的父类,方便用户的使用。
视图层的跳转
在struts-core jar包中打开struts-default.xml文件,它是一个struts2的默认配置文件。在里面可以找到:
<result-types>
<result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/>
<result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/>
<result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/>
<result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/>
<result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
<result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/>
<result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/>
<result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" />
</result-types>
它说明了向视图层跳转时有10种方式:
默认方式为dispatcher,不写时就代表默认方式;
redirectAction:Struts2 Action的跳转;
redirect: 与response.sendRedirect()相同;
dispatcher:与 RequestDispatcher forward(ServletRequest request,ServletResponse response)相同;
Struts2 Action方法的调用(三种):
1:在<action />中没有定义method属性,表示调用当前Action的execute();
<action name="index3" class="com.action.IndexAction3">
<result name="error">
/hello3.jsp
</result>
</action>
2: Wildcards
As an application grows in size, so will the number of action mappings. Wildcards can be used to combine similar mappings into one more generic mapping.
<action name="index1*" class="com.action.IndexAction1" method="{1}" >
<result name="success" type="redirectAction" >
index3
</result>
</action>
method中的1代表name中的第一个*;
3:Dynamic Method Invocation
Dynamic Method Invocation (DMI) will use the string following a "!" character in an action name as the name of a method to invoke (instead ofexecute). A reference to "Category!create.action", says to use the "Category" action mapping, but call thecreate method instead.
<action name="index2!*" class="com.action.IndexAction2" method="{1}">
<result name="success">
/hello2.jsp
</result>
</action>
method中的1代表name中的第一个*;
实例:
package com.action;
public class IndexAction1 {
public String msg(){
return "success";
}
}
---
package com.action;
import com.opensymphony.xwork2.Action;
public class IndexAction2 implements Action{
public String execute(){
return SUCCESS;
}
public String succ(){
return SUCCESS;
}
}
---
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction3 extends ActionSupport {
@Override
public String execute(){
return ERROR;
}
}
---
<?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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>
-->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="index1*" class="com.action.IndexAction1" method="{1}" >
<result name="success" type="redirectAction" >
index3
</result>
</action>
<action name="index2!*" class="com.action.IndexAction2" method="{1}">
<result name="success">
/hello2.jsp
</result>
</action>
<action name="index3" class="com.action.IndexAction3">
<result name="error">
/hello3.jsp
</result>
</action>
</package>
<!-- Add packages here -->
</struts>
---
测试:
分别用以下URL,注意观察和思考:
http://localhost:8080/struts2_0200_action/index3
http://localhost:8080/struts2_0200_action/index2!succ
http://localhost:8080/struts2_0200_action/index2
http://localhost:8080/struts2_0200_action/index1msg
注意:
<result name="error">
/hello3.jsp
</result>
name代表Action方法的返回值,缺省代表success;
type代表Action向视图跳转时的跳转类型,缺省代表plainText。
3:Struts2 Action(二)action 获取参数的三种方法
1:ModelDriven方式:
import com.domain.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class UserAction1 extends ActionSupport implements ModelDriven<User> {
private User user = new User();
public String add(){
System.out.println(user.getName());
System.out.println(user.getAge());
return "success";
}
public User getModel() {
return user;
}
}
import com.domain.User;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction2 extends ActionSupport {
private User user ;
public String add(){
System.out.println(user.getName());
System.out.println(user.getAge());
return "success";
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
import com.opensymphony.xwork2.ActionSupport;
public class UserAction3 extends ActionSupport{
private String name;
private String age;
public String add(){
System.out.println(name);
System.out.println(age);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
4:Struts2 Action(三) 访问web元素的四种方式
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction1 extends ActionSupport implements RequestAware,SessionAware,ApplicationAware {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public String add(){
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return "success";
}
public void setRequest(Map<String, Object> request) {
this.request=request;
}
public void setSession(Map<String, Object> session) {
this.session=session;
}
public void setApplication(Map<String, Object> application) {
this.application=application;
}
}
2: Map 依赖容器 方式:
package com.action;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction2 extends ActionSupport {
private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application;
public UserAction2() {
ActionContext context = ActionContext.getContext();
request=(Map<String, Object>)context.get("request");
session=context.getSession();
application=context.getApplication();
}
public String add(){
request.put("r2", "r2");
session.put("s2", "s2");
application.put("a2", "a2");
return "success";
}
}
3: Servlet IoC 方式:
package com.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction3 extends ActionSupport implements ServletRequestAware {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public String add() {
request.setAttribute("r3", "r3");
session.setAttribute("s3", "s3");
application.setAttribute("a3", "a3");
return "success";
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
this.session=request.getSession();
this.application=session.getServletContext();
}
}
4: Servlet 依赖容器 方式:
package com.action;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction4 extends ActionSupport {
private HttpServletRequest request;
private HttpSession session;
private ServletContext application;
public UserAction4() {
request = ServletActionContext.getRequest();
session=request.getSession();
application=session.getServletContext();
}
public String add(){
request.setAttribute("r4", "r4");
session.setAttribute("s4", "s4");
application.setAttribute("a4", "a4");
return "success";
}
}
- [Struts2]Struts2学习总结
- 【Struts2框架】Struts2总结
- 【Struts2】Struts2要点总结
- struts2 总结
- Struts2总结
- struts2总结
- Struts2总结
- struts2总结
- struts2总结
- Struts2总结
- Struts2总结
- struts2 总结
- struts2总结
- struts2总结
- Struts2总结
- STRUTS2总结
- struts2 总结
- struts2总结
- Thrift之代码生成器Compiler原理及源码详细解析1
- 尕福(散文)
- Thrift之代码生成器Compiler原理及源码详细解析2
- 和菜鸟一起学linux总线驱动之初识USB设备枚举
- Thrift之代码生成器Compiler原理及源码详细解析3
- struts2总结
- Hadoop Cluster 启动分析
- 深入探索 Kdump,第 1 部分
- 蝴蝶兰与鳞托菊
- Hadoop启动脚本与配置详解[转载]
- CSS3的使用技巧总结
- srand 函数
- 字符串去除特定字符
- MATLAB "Out of memory"问题