struts是流行和成熟的基于MVC设计模式的web应用程序框架,使用struts可以帮助我们减少运用MVC设计模型来开发web应用的时间。

目录:

一、struts2的工作原理及文件结构

二、三种访问Servlet API的方式

三、struts接收参数的三种方式

四、自定义拦截器

一、struts2的工作原理及文件结构

注:FilterDispatcher被替成StrutsPrepareAndExecuteFilter(如果使用FilterDispatcher过滤器时,程序员自己写的Filter过滤器必须放在所有过滤器的前面。而StrutsPrepareAndExecuteFilter过滤器可以让程序员在执行action之前写自己的Filter)

描述Struts流程:

网页产生HttpServletRequest请求->经过多个过滤器->到达ActionMaaper,判断是否是action请求(如果是)->通过StrutsPrepareAndExecuteFilter过滤器到达Actionproxy,一方面通过configuration Manager(配置管理器)读取struts.xml文档,另一方面创建一个实例,经过一系列的拦截器->执行到Action->返回result(对应了视图)->经过一系列的拦截器(逆序)->通过HttpServletResponse返回到用户实例。

二、三种访问servlet API的方法

struts2中没有提供任何一个servlet对象,不存在HttpServletRequest,HttpServletResponse对象。但是Struts2提供了三种方式间接的去访问Servlet API

1、ActionContext

通过ActionContext的getContext()静态方法获取ActionContext对象,通过ActionContext对象的一些getSession(),getApplication(),put()等方法,但是千万要注意的是,get获取到的对象都为Map键值对类型。com.opensymphony.xwork2.ActionContext

 public String execute() {
if ("ping".equals(username)) {
/*
* ActionContext可以获得Servlet对象 但是无法获得response响应对象获得
* 获得的request、session、Application 都是Map类型
*/ ActionContext.getContext().put("用户名", username);
Map session=ActionContext.getContext().getSession();
Map application=ActionContext.getContext().getApplication();
Map request=(Map)ActionContext.getContext().get(StrutsStatics.HTTP_REQUEST);
} else {
ActionContext.getContext().put("info", "信息");
}
return SUCCESS;
}

2、ServletActionContext

通过调用ServletActionContext类的一些包括getResponse(),getRequest(),getServletContext()等在内的静态方法,这些静态方法的返回类型是和Servlet中的对象类型是一一对应的。其中getResponse()返回类型为HttpServletResponse,getRequest()返回类型为HttpServletRequest().

 public String execute2() throws IOException {
if ("ping".equals(username)) {
HttpServletResponse response=ServletActionContext.getResponse();
4 HttpServletRequest request=ServletActionContext.getRequest();
HttpSession session=ServletActionContext.getRequest().getSession();
ServletContext application=ServletActionContext.getServletContext();
} else { }
System.out.println(username);
return SUCCESS;
}

3、实现xxxAware接口

(1)实现ServletRequestAware,ServletResponseAware,ServletSessionAware

 public class LoginAction extends ActionSupport implements ServletRequestAware

 private HttpServletRequest request;
//需实现方法 public void setServletRequest(HttpServletRequest request) { this.request=request; } //response示例
public String execute1() throws IOException {
if ("ping".equals(username)) {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<script type='text/javascript'>alert('验证码输入错误!')</script>");
out.print("<script type='text/javascript'>location.href='/index.jsp'</script>");
out.flush();
out.close();
} else {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.print("<script type='text/javascript'>alert('验证码输入错误!')</script>");
out.flush();
out.close();
}
System.out.println(username);
return SUCCESS;
}

(2)实现RequestWare、SessionWare、ApplicationWare等接口

public class LoginAction2 extends ActionSupport implements RequestAware,SessionAware, ApplicationAware {

    private Map<String, Object> request;
private Map<String, Object> session;
private Map<String, Object> application; //DI dependency injection
//IoC inverse of control
public String execute() {
request.put("r1", "r1");
session.put("s1", "s1");
application.put("a1", "a1");
return SUCCESS;
} @Override
public void setRequest(Map<String, Object> request) {
this.request = request;
} @Override
public void setSession(Map<String, Object> session) {
this.session = session;
} @Override
public void setApplication(Map<String, Object> application) {
this.application = application;
} }

三、struts三种接收参数方式

Struts有三种方式接收参数,且这三种方式都是自动完成赋值的setter方法。

1、使用Action的属性接收参数

代码:

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="LoginAction" method="login" class="com.third.LoginAction1">
<result>/loginSuccess.jsp</result>
</action>
</package> </struts>

login.jsp(登陆提示页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
<h1>login page</h1>
<form action="LoginAction.action" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"/></td>
</tr> <tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="reset"></td>
</tr> </table> </form>
</body>
</html>

loginSuccess.jsp(登陆成功提示界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>login success!</title>
</head>
<body>
<h1>login success!</h1>
</body>
</html>

LoginAction.java

 package com.third;

 import com.opensymphony.xwork2.ActionSupport;

 public class LoginAction1 extends ActionSupport {

     private String username;
private String password; public String login(){
//这里能够打印出来传入的值,则说明能够自动调用setter方法完成赋值
System.out.println("username:"+username+" password:"+password);
return SUCCESS;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

运行结果截图:

2、使用DomainModel接收参数

注:这里在表单传值是,必须指明这个属性值,到底穿个action中的那个引用,例如user.username.

代码:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" method="login" class="com.third.LoginAction">
<result>/loginSuccess.jsp</result>
</action>
</package> </struts>

login.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>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login page!</h1>
<form action="LoginAction.action" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="user.username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="user.password"/></td>
</tr> <tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>

loginSuccess.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>My JSP 'loginSuccess.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login success!</h1>
</body>
</html>

User.java

package com.third;

public class User {

    private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

LoginAction.java

 package com.third;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.inject.Context; public class LoginAction extends ActionSupport { private User user;
public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
}
public String login(){
//这里可以打印出传入的值的话,Action完成了自动调用setter方法赋值
System.out.println("username:"+user.getUsername()+" password:"+this.getUser().getPassword());
return SUCCESS;
} }

运行结果截图:

3、使用ModelDriven接受参数

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <package name="default" namespace="/" extends="struts-default">
<action name="LoginAction" method="login" class="com.third.LoginAction">
<result>/loginSuccess.jsp</result>
</action>
</package> </struts>

login.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>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login page!</h1>
<form action="LoginAction.action" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"/></td>
</tr> <tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>

loginSuccess.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>My JSP 'loginSuccess.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login success!</h1>
</body>
</html>

User.java

package com.third;

public class User {

    private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

LoginAction.java

package com.third;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.inject.Context; public class LoginAction extends ActionSupport implements ModelDriven<User> { private User user=new User();
public String login(){
//这里打印出传入的参数值,说明自动调用setter方法赋值
System.out.println("username:"+user.getUsername()+" password:"+user.getPassword());
return SUCCESS;
} @Override
public User getModel() { return user;
} }

运行结果截图:

四、自定义拦截器

注:特别要注意在使用拦截的器的时候,使用表单传值,会导致Action中获得的属性的值为null,或是其他的默认的初始化值。

1、实现Interceptor接口

-void init()方法:初始化拦截器所需要的资源

-void destory()方法:释放init()中分配的资源

-String intercept(ActionInvocation invocation)throws Exception:实现拦截器功能,利用ActionInvocation参数获取Action状态,返回result字符串作为逻辑视图。

代码:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <package name="default" namespace="/" extends="struts-default">
<!--注册拦截器 -->
<interceptors>
<interceptor name="timefigureFilter" class="com.third.TimeInterceptor">
</interceptor>
</interceptors> <action name="LoginAction" method="login" class="com.third.LoginAction">
<result>/loginSuccess.jsp</result>
<interceptor-ref name="timefigureFilter"></interceptor-ref>
</action>
</package> </struts>

login.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>My JSP 'login.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login page!</h1>
<form action="LoginAction.action" method="post">
<table>
<tr>
<td>username:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"/></td>
</tr> <tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="reset"></td>
</tr>
</table>
</form>
</body>
</html>

loginSuccess.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>My JSP 'loginSuccess.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>login success!</h1>
</body>
</html>

User.java

package com.third;

public class User {

    private String username;
private String password; public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

TimeInterceptor.java

 package com.third;

 import javax.servlet.http.HttpServletRequest;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class TimeInterceptor implements Interceptor { @Override
public void destroy() {
// TODO Auto-generated method stub } @Override
public void init() {
// TODO Auto-generated method stub } @Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request=ServletActionContext.getRequest();
String username=request.getParameter("username");
String password=request.getParameter("password");
request.setAttribute("username", username);
//1执行action之前
long start=System.currentTimeMillis();
//2执行下一个拦截器,直到最后一个拦截器,则执行目标Action
String result=invocation.invoke();
//3执行完action之后
long end=System.currentTimeMillis();
System.out.println("执行Action花费的时间为:"+(end-start)+"ms");
return result;
} }

LoginAction.java

 package com.third;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.inject.Context; public class LoginAction extends ActionSupport implements ModelDriven<User> { private User user=new User();
public String login(){
//着重看着两行代码运行打印的结果
System.out.println("username:"+user.getUsername());
//System.out.println("username:"+ServletContext.getRequest().getAttribute("username");//这里也可完成正常的获值
System.out.println("username:"+ServletActionContext.getRequest().getAttribute("username"));
return SUCCESS;
} @Override
public User getModel() { return user;
} }

运行结果截图:

注:注意看结果,打印的第一个username的属性值为null,而第二个为表单填写的dasd。这里涉及到拦截器的使用导致的表单传值问题。

分析:拦截器的使用导致的表单传值问题

原因:登陆界面填写完成之后,表单需要实现页面跳转,而这些将会交给struts,struts在调用action的过程其实是调用action中的struts.xml配置action标签method属性指定的方法(默认是execute()),而在调用这个方法前会对表单的属性信息将会别匹配赋值给action中同名属性。正常情况下没有自定义的拦截器,通过表单传递的属性值没有问题。

要完成这个功能,有很大程度上, Struts 2 要依赖与 ValueStack 对象。这个对象贯穿整个 Action 的生命周期 (每个 Action 类的对象实例会拥有一个 ValueStack 对象 ) 。当 Struts 2 接收到一个 action 的请求后,会先建立Action 类的对象实例,但不会调用 Action 方法,而是先将 Action 类的相应属性放到 ValueStack 对象的顶层节点(ValueStack 对象相当于一个栈 ). 只是所有的属性值都是默认的值,如 String 类型的属性值为 null,int 类型的属性值为 0 等。

在处理完上述工作后, Struts 2 就会调用拦截器链中的拦截器,当调用完所有的拦截器后,最后会调用 Action 类的 Action 方法,在调用 Action 方法之前,会将 ValueStack 对象顶层节点中的属性值赋给 Action 类中相应的属性。大家要注意,在这里就给我们带来了很大的灵活性。也就是说,在 Struts 2 调用拦截器的过程中,可以改变ValueStack 对象中属性的值,当改变某个属性值后, Action 类的相应属性值就会变成在拦截器中最后改变该属性的这个值。

好多废话~总结就是:在拥有自定义的过滤器时,表单传属性值会先赋值给Action中属性,当运行完过滤器时,才会调用Action中的方法,在调用之前会将 ValueStack 对象中的默认初始化值赋给action中的属性,然后调用action中的方法,这样action中的属性全是ValueStack 对象中的默认初始化值。

解决的方法:间接访问Servlet的API,通过request对象去访问属性和属性值。如:

HttpServletRequest request=ServletActionContext.getRequest();

String username=request.getParameter("username");

2、方式二:继承AbstractInterceptor类

-提供了init()和destroy()方法的实现

-只需要实现intercept()方法即可

代码:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts> <package name="default" namespace="/" extends="struts-default">
<!--注册拦截器 -->
<interceptors>
<interceptor name="timefigureFilter1" class="com.third1.TimeInterceptor">
</interceptor>
</interceptors> <action name="TimeAction" class="com.third1.TimeAction">
<result>/index.jsp</result>
<!--引用拦截器 -->
<interceptor-ref name="timefigureFilter1"></interceptor-ref>
</action> </package> </struts>

TimeInterceptor.java

 package com.third1;

 import org.apache.struts2.ServletActionContext;

 import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class TimeInterceptor extends AbstractInterceptor { @Override
public String intercept(ActionInvocation invocation) throws Exception {
//1.执行action之前
long start=System.currentTimeMillis();
//2.执行下一个拦截器,如果已经是最后一个拦截器,则执行目标Action ServletActionContext.getRequest().setAttribute("username", "小帅哥");
ServletActionContext.getRequest().getSession().setAttribute("username","小美女");
ServletActionContext.getServletContext().setAttribute("username", "少艾");
String result=invocation.invoke();
//3.执行Action之后
long end=System.currentTimeMillis();
System.out.println("执行Action花费的时间:"+(end-start)+"ms");
//获取request对象,调用其setAttribute函数,将时间作为属性保存到request对象中
ServletActionContext.getRequest().setAttribute("time", (end-start));
ServletActionContext.getRequest().getSession().setAttribute("time1",(end-start));
ServletActionContext.getServletContext().setAttribute("time2", (end-start));
return result;
} }

TimeAction.java

 package com.third1;

 import com.opensymphony.xwork2.ActionSupport;

 public class TimeAction extends ActionSupport {

     @Override
public String execute() throws Exception{ for(int i=0;i<3;i++){
Thread.sleep(1000);
}
return SUCCESS;
}
}

index.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>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <h1>执行Action用户名:<%=request.getAttribute("username") %>(通过request方式获取)</h1>
<h1>执行Action用户名:<%=session.getAttribute("username") %>(通过session方式获取)</h1>
<h1>执行Action用户名:<%=application.getAttribute("username") %>(通过application方式获取)</h1> <h1>执行Action花费的时间:<%=request.getAttribute("time") %>(通过request方式获取)</h1>
<h1>执行Action花费的时间:<%=session.getAttribute("time1") %>(通过session方式获取)</h1>
<h1>执行Action花费的时间:<%=application.getAttribute("time2") %>(通过application方式获取)</h1>
</body>
</html>

运行结果截图:

分析:对比两种颜色标记的代码区,对比一下他们,并且结合结果进行分析。我们不难发现过滤器的调用分为两个阶段,第一个阶段是在产生HttpServletRequest请求之后、action方法调用之前;第二阶段是产生与action之后,jsp页面(Template显示)跳转之后,HttpServletResponse之前。所以当你在 String result=invocation.invoke();   这条语句之后通过request,application,session给它设置属性值,将会没有起到任何作用。

可以结合struts2的工作原理及文件结构图进行参考。

struts2基础——需要注意的几点的更多相关文章

  1. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  2. Struts2基础学习2

    Struts2基础学习2 项目结构,测试页面与实体类 <%@ page language="java" contentType="text/html; charse ...

  3. Struts2基础入门

    Struts2基础入门 创建一个web工程 0)导包并且创建一个核心配置文件 <?xml version="1.0" encoding="UTF-8"?& ...

  4. Struts2基础学习总结

    引用自:http://www.cnblogs.com/jbelial/archive/2012/05/10/2486886.html Struts 2是在WebWork2基础发展而来的. 注意:str ...

  5. 经典MVC框架技术-struts2基础知识

    Struts2框架简介 struts2框架是在struts1和webwork技术的基础上,进行合并的全新框架,struts2以Webwork为核心,采用拦截器来处理用户的请求,这样的设计使得业务逻辑控 ...

  6. Struts2框架学习第三章——Struts2基础

    本章要点 —  Struts 1框架的基本知识 — 使用Struts 1框架开发Web应用 —  WebWork框架的基本知识 — 使用WebWork框架开发Web应用 — 在Eclipse中整合To ...

  7. (一)Struts2 基础

    一.Struts简介 1.1 历史 虽然Struts 2号称是一个全新的框架,但这仅仅是相对Struts 1而言.Struts 2与Struts 1相比,确实有很多革命性的改进,但它并不是新发布的新框 ...

  8. struts2 基础

    框架(frameWork):某一种应用的半成品 struts2: 表现层 处理与页面进行交互的相关功能  hibernate: 持久层 负责业务逻辑数据的持久化  spring: 业务层 负责复杂的业 ...

  9. struts2 基础学习

      Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts ...

  10. 2. Struts2 基础

    1. Struts2简介 Struts2是一个WEB端MVC框架.作为比较早的MVC 框架之一,Struts2在使用中还是比较多的.虽然个人感受没有SpringMVC还那么的好用 Struts2 官网 ...

随机推荐

  1. java简易excel导入导出工具(封装POI)

    Octopus 如何导入excel 如何导出excel github项目地址 Octopus Octopus 是一个简单的java excel导入导出工具. 如何导入excel 下面是一个excel文 ...

  2. 谈谈对XML的理解?说明Web应用中Web.xml文件的作用?

    谈谈对XML的理解?说明Web应用中Web.xml文件的作用? 解答:XML(Extensible Markup Language)即可扩展标记语言,它与HTML一样,都是SGML(Standard ...

  3. 嵌入式驱动开发之phy---fine Mac与Phy组成原理的简单分析

    关键字rj45.pci-e 1. general 下图是网口结构简图.网口由CPU.MAC和PHY三部分组成.DMA控制器通常属于CPU的一部分,用虚线放在这里是为了表示DMA控制器可能会参与到网口数 ...

  4. 项目文件不完整。缺少预期导入,DotnetCore如何切换SDK版本

    1. 项目文件不完整.缺少预期导入 如图: 2. 出现原因: SDK版本与项目所需的SDK版本不一致. 3. 解决问题: 3.1 项目所需的SDK版本如何确定? a. 检查.sln文件所在目录是否有: ...

  5. 【Raspberry pi】GPIO注意事项

    1.GPIO编码的方法 第三列是树莓派板子上的自然编号(左边引脚为1-15,右边引脚为2-26),RPi.GPIO.setmode(GPIO.BOARD)采用这列编号 树莓派主芯片提供商Broadco ...

  6. simpson公式求定积分(模板)

    #include<cstdio> #include<cmath> #include <algorithm> using namespace std; double ...

  7. JZOJ.5230【NOIP2017模拟8.5】队伍统计

    Description 现在有n个人要排成一列,编号为1->n .但由于一些不明原因的关系,人与人之间可能存在一些矛盾关系,具体有m条矛盾关系(u,v),表示编号为u的人想要排在编号为v的人前面 ...

  8. 【BZOJ4503】两个串 FFT

    [BZOJ4503]两个串 Description 兔子们在玩两个串的游戏.给定两个字符串S和T,兔子们想知道T在S中出现了几次, 分别在哪些位置出现.注意T中可能有“?”字符,这个字符可以匹配任何字 ...

  9. [hihoCoder] KMP算法

    Each time we find a match, increase the global counter by 1. For KMP, algorithm, you may refer to th ...

  10. Less-mixin函数基础一

    //mixin函数 立即执行mixin函数,example: .test{ color:#ff00000; background:red; } //立即执行mixin grammar 1 扩展exte ...