Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58
常用的listener有:
  (1) ServletContextListener 监听ServletContext。
        当创建ServletContext时,激发 contextInitialized(ServletContextEvent sce)方法;
       当销毁ServletContext时,激发contextDestroyed(ServletContextEvent sce)方法。

  (2)ServletContextAttributeListener监听对ServletContext属性的操作,比如增加、删除、修改属性。

  (3)HttpSessionListener 监听HttpSession的操作。
          当创建一个Session时,激发 session Created(HttpSessionEvent se)方法;
          当销毁一个Session时,激发sessionDestroyed (HttpSessionEvent se)方法。
  

 (4)HttpSessionAttributeListener 监听HttpSession中的属性的操作。
        当在Session增加一个属性时,激发 attributeAdded(HttpSessionBindingEvent se) 方法;
        当在Session删除一个属性时,激发attributeRemoved(HttpSessionBindingEventse)方法;
        当在Session属性被重新设置时,激发attributeReplaced(HttpSessionBindingEvent se) 方法。

1,  SerlvetContextListener 

(1) 在每一个java的web应用中都只能又一个单独的servlet context。而这个context被放置在整个应用级别上,当应用一旦启动之后,servlet容器就会创建这样一个servlet context来服务于web应用。因此我们也可以创建一个类并实现了一个ServletContextListerner接口,用于通知这个context是否被创建了或者被销毁了。这样当Context被创建时,就会收到一个相关的通知,并且在Context被销毁前也会得到一个通知。
如: public void contextInitialized(ServletContextEvent event) {
ServletContext ctx = event.getServletContext( );
ctx.setAttribute("dateStarted", new Date( ));
//在页面或程序中用<%=getServletContext().getAttribute("dateStarted")%> 得到设置的属性
}
注:在使用Struts的时候,我们可以采用plug-in的方式来解决这个问题。因为pulg-in可以做context Listern所能做的任何事情。并且能够传入各种配置在struts-config.xml文件中的参数。
(2)  在web.xml中配置

   <listener>
       <listener-class>demo.listener.MyContextLoader</listener-class>
  </listener>
(2) 定时任务的实现
import java.util.Timer;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyListener implements ServletContextListener {

private Timer timer = null;

public void contextInitialized(ServletContextEvent event) {
timer = new Timer(true);
//设置任务计划,启动和间隔时间
timer.schedule(new MyTask(), 0, 86400000);
}

public void contextDestroyed(ServletContextEvent event) {
timer.cancel();
}

}

class MyTask extends TimerTask {

public void run() {
// System.out.println("call at " + (new Date()));
// TODO 此处添加具体任务代码
}

2, HttpSessionListener

(1) 用来监听Servlet Context的创建和销毁的状态
(2)统计当前有多少在线用户,原理:看创建了多少个session即可

package demo.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class SessionCounter implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent event) {
ServletContext ctx = event.getSession( ).getServletContext( );
Integer numSessions = (Integer) ctx.getAttribute("numSessions");

//在页面或程序中用<%=getServletContext().getAttribute("dateStarted")%> 得到人数

if (numSessions == null) {
    numSessions = new Integer(1);
}
else {
    int count = numSessions.intValue( );
    numSessions = new Integer(count + 1);
}
ctx.setAttribute("numSessions", numSessions);
}
public void sessionDestroyed(HttpSessionEvent event) {
ServletContext ctx = event.getSession( ).getServletContext( );
Integer numSessions = (Integer) ctx.getAttribute("numSessions");
if (numSessions == null) {
    numSessions = new Integer(0);
}
else {
    int count = numSessions.intValue( );
    numSessions = new Integer(count - 1);
}
ctx.setAttribute("numSessions", numSessions);
}
}
3, HttpSessionAttributeListener
(1) 监听session中的属性的创建,移除等,如当创建userName属性时做些什么之类
(2)应用:

package demo.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;

public class UserCounter implements HttpSessionAttributeListener {

public void attributeAdded(HttpSessionBindingEvent event) {
if (attributeIsUser(event))
    adjustUserCounter(event.getSession( ).getServletContext( ),
    true);
}

public void attributeRemoved(HttpSessionBindingEvent event) {
if (attributeIsUser(event))
    adjustUserCounter(event.getSession( ).getServletContext( ),
    false);
}

public void attributeReplaced(HttpSessionBindingEvent event) {
}

private boolean attributeIsUser(HttpSessionBindingEvent event) {
String name = event.getName( );
Object value = event.getValue( );
return "user".equals(name) &&
       value instanceof demo.app.domain.user;
}

private void adjustUserCounter(ServletContext ctx, boolean userAdded) {
Integer counterObj = (Integer) ctx.getAttribute("numUsers");
int counter = (counterObj == null ? 0 : counterObj.intValue( ));
if (userAdded) {
  counter++;
}
else {
    if (counter > 0) counter--;
}
ctx.setAttribute("numUsers", new Integer(counter));
}
}


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

web.xml “/ “代表当前工程根目录,如工程名为testSSH, 则在web.xml中用/WEB-INF/applicationContext.xml 代表
testSSH//WEB-INF/applicationContext.xml


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

查看tomcat下webapp知:编译好的class文件会放到WEB-INF的classes之下,
? 即:若src 下有一applicationContext.xml文件,实际运行地址为:/WEB-INF/classes/applicationContext.xml
?src源文件夹位置与WEB-INF下的lib文件夹平行。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

Ctrl + shift +R
这可能是所有快
8 @( c% F, k” t” v9 Q$ m9 {$ g捷键组合中最省时间的了。这组快捷键可以让你打开你的工作区中任何一个文件

ctrl+.及ctrl+1:下一个错误及快速修改 ,F2显示错误3 g’ t+ J??F1 q) ]9 J” g* a

ctrl+o:快速outline
0 h$ ?% M& ?8 |9 z5 W9 ~” |??c如果想要查看当前类的方法或某个特定方法,但又不想把代码拉上拉下,也不想使用查找功能的话,就用ctrl+o吧。它可以列出当前类中的所有方法及属性,你只需输入你想要查询的方法名,点击enter就能够直接跳转至你想去的位置。% @# F8 c; j, D- c* ^

Ctrl + Q 回到上次编辑处
Ctrl + ↓ 向下
shift + ← 从当前位置选中
Alt+↓ 当前行和下面一行交互位置
Alt+← 前一个编辑的页面
Ctrl + Alt + ↓ 复制当前行到下一行 接着按Alt+↓ 完成一行的复制

Ctrl+Enter,Shift+Enter在当前行的下一行插入空行(鼠标可以在当前行的任一位置,不一定是最后)
Shift+Ctrl+Enter 在当前行插入空行(原理同上条)
Ctrl+D删除当前行。
Ctrl+L 定位在某行

Ctrl+2 快速替换
Alt+Shift+R 重命名

F12 Activate Editor (启动编辑器) : ?
Ctrl+M切换窗口的大小
Ctrl+W 关闭当前Editer
Ctrl+E 打开窗口浏览,浮动的方式在编辑区
Ctrl+Shift+E Switch to Editor (切换至编辑器) :
Ctrl+F6? 切换至下一个编辑器
Ctrl+Shift+F6 切换至上一个编辑器
Ctrl+F7? 切换至下一个视图,如server 与 console切换
Ctrl+Shift+F7 上一个
Ctrl+F8? 切换至下一个视景,如hibernate 与 web 视景
Ctrl+Shift+F8 上一个

F2 持续显示Show Tooltip Description
F3跳到声明或定义的地方。

Ctrl+/ 在代码窗口中是这种//~注释。
Ctrl+Shift+/ 在代码窗口中是这种/*~*/注释

Ctrl+O 快速显示 OutLine
Ctrl+T 快速显示当前类的继承结构
Ctrl+K 参照选中的Word快速定位到下一个

Ctrl+Shift+F格式化文件Format Document。
Ctrl+Shift+P 定位到对于的匹配符(譬如{}) (从前面定位后面时,光标要在匹配符里面,后面到前面,则反

之)

Ctrl+Shift+M(先把光标放在需导入包的类名上) 作用是加Import语句。
Ctrl+Shift+O作用是缺少的Import语句被加入,多余的Import语句被删除。

在.jap.或.java等文件中右键选“Campare With”或“Re place With”可以找到所有操作的历史记录。

Ctrl+Space 代码助手完成一些代码的插入(但一般和输入法有冲突,可以修改输入法的热键,也可以暂用

Alt+/来代替)

Ctrl+J 正向增量查找(按下Ctrl+J后,你所输入的每个字母编辑器都提供快速匹配定位到某个单词,如果没

有,则在stutes line中显示没有找到了,查一个单词时,特别实用,这个功能Idea两年前就有了)
Ctrl+Shift+J 反向增量查找(和上条相同,只不过是从后往前查)

Alt+Shift+C 修改函数结构(比较实用,有N个函数调用了这个方法,修改一次搞定)
Alt+Shift+Z 重构的后悔药(Undo)


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

jvm内存分配不够造成
myeclipse 配置server 选择用到的服务器,自带的在Servers/Integrated Sandbox下
MyEclipse Tomcat /JDK Optional Java VM arguments:增加
-Xms128m
-Xmx256m
-XX:PermSize=128M
-XX:MaxNewSize=256m
-XX:MaxPermSize=256m

-Xms40m:虚拟机占用系统的最小内存
-Xmx256m:虚拟机占用系统的最大内存
-XX:PermSize:最小堆大小。一般报内存不足时,都是说这个太小,
?? ?? ?? ?? ?? ?? ??? 堆空间剩余小于5%就会警告,建议把这个稍微设
?? ?? ?? ?? ?? ?? ?? ?? ?? 大一点,不过要视自己机器内存大小来设置
-XX:MaxPermSize:最大堆大小。这个也适当大些


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

namespace=”/news”? “/news”是在地址栏中显示的路径,而非目录 如:http://pc-officea3:8080/news/index.action?
在jsp中用index.action 相当于 /news/index.action 要结合地址栏中名称空间来找action


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1, 美体学堂 在链接中执行action,根据action转到指定的页面
这样传的只是admin.id,即使此链接在一个form内,也不会传form内的数据
若想传form内的数据,那么唯一的方法就是让form提交
即: 删除

ps: form传参


这样提交时数据传的是form包裹的数据,在form外的是不会被提交的。

2,

若用 /manage/news/insert.action 且在tomcat 中path=“/” 则为绝对路径,否则代表当前目录下的manage文件夹下action


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

菜单
Project -> Build Automatically
Myeclipse中Tomcat的两种部署方式
一、在Myeclipse软件中部署

1、 在Myeclipse中,创建好工程后,在Myeclipse菜单栏中选择 Windows -> Preferences -> Myeclipse -> Tomcat5,选择”Enable”;Tomact Home Directory要选择你机器上Tomcat所安装的目录。然后,再Windows -> Preferences -> Myeclipse -> Tomcat5 ->JDK,确保这个是有对应的jdk版本,若没有,则通过”Add”添加其安装路径。ok

2、选择工具栏上的按钮,在”Project”中选择你要部署的工程,然后”Add”,再选择”Tomcat”,Finish。

3、 在工具栏上选择,启动Tomcat。

4、 运行程序。在IE中输入http://localhost:8080/aa/WebRoot/index.jsp(aa为工程名字)
二、在Tomcat的server.xml配置文件中部署

1、在Tomcat下,找到conf文件下的server.xml,打开。

2、在<Host>和</host>之间加上如下代码:

1
2 <Context path=”/虚拟目录名” docBase=”目标目录位置” debug=”0″ reloadable=”true” >
3 </Context>

(严格区分大小写,当path=””时,此时的虚拟目录直接为root—>http://localhost:8080/)
参数:

?path:表示在浏览器中输入http://localhost:8080/虚拟目录名

如:path=”/aa” 则浏览器中输入http://localhost:8080/aa/index.jsp

? path 就是提供一个前缀功能。
?docBase:表示你的程序包的物理绝对路径,默认的绝对路径是???? %tomcat_home%/webapps

workDir:?? 表示是缓存文件的放置地点,可以方便跨平台移植时不用重编译。这样,你的应用程序就可以放到硬盘上的任意地方了,临时文件的存放地点不设置的话,默认存放在tomcat\work\Catalina\\localhost之下\localhost

?reloadable :为true,则tomcat会自动检测应用程序的/WEB-INF/lib 和/WEB-INF/classes目录的变化,自动装载新的应用程序,我们可以在不重起tomcat的情况下改变应用程序;为false则不自动加载。

如:
<Context path=”/” reloadable=”true” docBase=”E:\workspace\.metadata\.plugins\com.genuitec.eclipse.easie.tomcat.myeclipse\tomcat\webapps\testAuto”
workDir=”E:\workspace\.metadata\.plugins\com.genuitec.eclipse.easie.tomcat.myeclipse\tomcat\webapps\testAuto” />

在地址栏中输入:? http://localhost:8080/index.jsp 即可运行 ,不用再加上工程名如: http://localhost:8080/myproject/index.jsp??? </Host>
reloadable=”true”会自动更新classes, jsp文件??? ps: docBase要精确到工程名才可
用myeclipse 更改类文件时,其会自动编译,而tomcat就会自动更新classes文件,

问题:IE中没有显示变化结果,原因:myeclipse自动编译还未完成,在部分是很快的。
默认为false, 只会自动更新jsp文件

ps:路径要与path设置的路径相同才可看到自动发生的变化


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1, commons-fileupload-1.2.1.jar
2, commons-io-1.3.2.jar
3, commons-logging-1.0.4.jar
4, freemarker-2.3.13.jar
5, ognl-2.6.11.jar
6, struts2-core-2.1.6.jar
7, xwork-2.1.2.jar


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?将html元素值恢复成默认值,如 <input type=”text” value = “${news.title}”>之类。 若此文本框接收的是Action传来的值作为默认值,修改后,按下reset就会恢复成此默认值,而不是null


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

?须将表中所有的字段在jsp页面中接收,不想显示出来的就将其放入<input type=”hidden” value=”表字段”>
这样在提交时,更新Action的实体类,不会因未接收一个字段,使得此字段在实体类中为Null,覆盖了原有的数据


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1,在Action中:(ServletActionContext.getRequest().setAttribute
?????? 注:在配置action转发类型时要为dispatcher,不能为redirect, 因redirect忽略request的参数!
?2, 在页面中:${news.title}
?
forward 与 redirect 区别:

1.从地址栏显示来说
forward 地址栏还是原来的地址.
redirect 地址栏显示的是新的URL

2.从数据共享来说
forward:? 转发页面和转发到的页面可以共享request,session里面的数据.
redirect: 转发页面和转发到的页面不可以共享request里面的数据,但可以共享
session里面的数据.

3.应用
forward 是服务器内部的一种操作,只能在同一个Web应用程序内的资源之间转发
请求.
redirect 是服务器通知客户端,让客户端重新发起请求,不仅可以重定向到当前
应用程序的其他资源,还可以重定向到同一个站点上的其他应用程序中的资源,
甚至是使用绝对URL重定向到其他站点的资源.

4.从效率来说
forward:高.
redirect:低.

forward是服务器请求资源,服务器直接访问目标地址的URL,把那个URL的响应内容
读取过来,然后把这些内容再发给浏览器.浏览器根本不知道服务器发送的内容从
哪里来的,所以它的地址栏还是原来的地址.

redirect是服务端根据逻辑,发送一个状态码,(不会传request,故其不能共用
request中的数据)告诉浏览器重新去请求那个地址.所以地址栏显示的是新的
URL.所以redirect等于客户端向服务器端发出两次request,同时也接受两次
response。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

Action中只要定义一个变量,及set,get方法,那么当页面传来数据,变量名相同时就会调用Action的set方法进行赋值
在页面中接收Action数据时,会将Action的数据自动存入到request中,只要没设置type=”redirect”,那么在页面中直接用${userName}就会自动得到数据。

页面 ————request ———Action ————request —————页面


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1, Action中传送

方式(1) Collection actionMessage = new ArrayList ();
actionMessage.add(“输入有误!”);
setActionMessages(actionMessage);

方式(2)addActionMessage(“输入有误!”)

2,Struts.xml 中配置

success.jsp
/error.jsp

3,页面接收

注意:

如果将result标签的type属性设置为redirect则在Action即使使用了

addActionMessage(“输入有误!”);
setActionMessages(actionMessage);

在JSP页面也不能显示相应的信息


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1,WEB执行流程:发送jsp页面请求,Servlet接收,读取web.xml文件 ,据传来的路径转到对应的Servlet,进行操作
Servlet向jsp页面输出,会得到执行的是文字,图片之类而form指定的action因还没有submit,故此时不会得到执行
因图片是要读取加载的,故可在其中令其执行action
如:
即:页面显示时执行action

2,在Web.xml中配置:

index.action


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

相同的部分:
客户端发送jsp页面请求,Servlet接收,读取web.xml文件 ,据传来的路径转到对应的Servlet,进行操作,
不同在于操作时方法名是否固定,是否须extends框架提供的类
servlet要自定义接收参数,getParameter, 手动设置响应返回,
PrintWriter out = response.getWriter();
out.print( “

” );
而struts接收数据已由框架本身完成接受,而转到某一页面则可指定,不是手动再创建。

在web.xml三种框架的配置比较

ps: 任意指定,如可以都为*.action

1, Servlet

AutoServlet

com.servlet.AutoServlet



AutoServlet
/AutoServlet

AutoServlet extends HttpServlet,实现其get,post方法
当jsp路径为/AutoServlet时执行get方法,
可看出:所有的操作类都要在web.xml中配置,不能集上管理这些servlet,而Struts分别提供了struts-config.xml,struts.xml集中管理操作类的文件

执行的方法固定,get,set

2,Struts1.x

action
org.apache.struts.action.ActionServlet
config /WEB-INF/struts-config.xml


action
*.do

当页面传来*.do时执行action,这是Struts系统提供的Servlet,它会读取中所配置的
/WEB-INF/struts-config.xml文件,进一步找到具体的action进行操作。

action 方法名为execute, 若其它任意,不过必须继承Struts1.x提供的action类

3,Struts2.0

struts2

org.apache.struts2.dispatcher.FilterDispatcher



struts2
*.action

当页面传来*.action时执行FilterDispatcher,它会读取/src/struts.xml的文件,从而执行对应的类
action方法名任意,类继承谁没有限制,也就是说任何类都可以成为一个Struts1.x中的action

框架名称 是否继承特定类 方法名 集中管理action类文件

servlet 是,HttpServlet get, post 无, 一切都在web.xml中配置

struts1.x 是,Action,DispatchAction execute, 任意 有,struts-config.xml

struts2.0 任意 任意 有,struts.xml

struts1.x struts2.0 细节区别:

(1), 类形式:struts2.0的action 类似于struts1.x中的DispatchAction 不过前者类随意继承,没有限制,而后者强制要继承自Dispath才可执行.
(2), 运行: struts2.0的action可直接在 地址栏中用 http://pc-officea3:8080/testWeb/index.action.执行测试,而struts1.x必须要有jsp页面才可
(3), 测试:struts1.xservlet API execute方法I execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)暴露了servlet AP,在测试时须在jsp页面中提交,生成request才可进行, 而struts2.0 方法名是任意的,不限制的如insert()无形参,可直接用JUNITT等生成对象,用 myobject.set来给值,此步相当于jsp页面提交,最大的好处是不用转到jsp页面,生成request才能得到执行,因struts2.0 action是任意类,故可如一般类一样进行测试!!


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

Struts 2 Action类可以实现一个Action接口,也可实现其他接口,使可选和定制的服务成为可能。Struts2提供一个ActionSupport基类去实现 常用的接口。Action接口不是必须的,任何有execute标识的POJO对象都可以用作Struts2的Action对象
ps: pojo , 简单的Java对象(Plain Ordinary Java Objects)实际就是普通JavaBeans,使用POJO名称是为了避免和 EJB混淆起来, 而且简称比较直接.

Action类没有直接与request,response打交道,已经与Servlet API完全分离,可通过方法名形参看出来,但我们在实现业务逻辑处理时经常需要访问Servlet中的对象,如Session、Application等。Struts2.0 提供了一个名字为ActionContext的类,在Action中可以通过该类获得Servlet API。

ActionContext是一个Action的上下文对象,Action运行期间所用到的数据都保存在ActionContext中(如Session,客户端提交的参数等信息)。在每次执行Action之前都会创建新的ActionContext,ActionContext是线程安全的,也就是说在同一个线程里ActionContext里的属性是唯一的,这样我的Action就可以在多线程中使用。

在Action中可以通过下面的代码来创建和使用ActionContext类,关于该类的方法介绍如下所示:
ActionContext ac=ActionContext.getContext();

以下是ActionContext类的常用方法
1.Object get(Object key) :通过参数key来查找当前ActionContext中的值
2.Map getApplication() :返回一个Application级的Map对象
3.Static ActionContext getContext() :获得当前线程的ActionContext对象
4.Map getParameters() :返回一个包含所有HttpServletRequest参数信息的Map对象

5.Map getSession() :返回一个Map类型的HttpSession对象

我们取得的session却是Map类型的对象,这是为什么?框架将与Web相关的很多对象重新进行了包装,比如这里就将 HttpSession对象重新包装成了一个Map对象,供我们的Action使用,而不用直接和底层的HttpSession打交道。也正是框架的包装,让我们的Actoion可以完全的和Web层解藕。

6.Void put(Object key,Object value) :向当前ActionContext对象中存入名值对信息
7.Void setApplication(Map application) :设置Application上下文
8.Void setSession(Map session) :设置一个Map类型的Session值

如果我们的Action需要直接与JavaServlet的HttpSession、HttpServletRequest等一些对象进行操作,我们又该如何处理?请看下面的ServletActionContext。

ServletActionContext
这个类直接继承了我们上面介绍的ActionContext,它提供了直接与JavaServlet相关对象访问的功能

1、取得HttpServletRequest对象:

HttpServletRequest request = ServletActionContext. getRequest();

2、取得HttpSession对象:
HttpSession session = ServletActionContext. getRequest().getSession();

ServletActionContext和ActionContext有着一些重复的功能,在我们的Action中,该如何去抉择呢?我们遵循的原则是:如果ActionContext能够实现我们的功能,那最好就不要使用ServletActionContext,让我们的Action尽量不要直接去访问JavaServlet的相关对象。在使用ActionContext时有一点要注意:不要在Action的构造函数里使用 ActionContext.getContext(),因为这个时候ActionContext里的一些值也许没有设置,这时通过 ActionContext取得的值也许是null。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

1, dispatcher -forward 转到指定的jsp页面
2,redirect 重定向到jsp页面或action
3,chain 由当前action转到另一action, 形成一个action链,彼些间共享一个ActionContext,在一个action中设置ActionContext属性,可被其它action共享。

redirect 与 dispatcher的区别与 reidrect,forward相似

一、dispatcher

(1)为缺省的result类型,一般情况下我们在struts.xml会这么写:
Xml代码

1. /main.jsp

/main.jsp

以上写法使用了两个默认,其完整的写法为:
Xml代码

1.
2. /maini.jsp 3.

/maini.jsp

第一个默认:type=”dispatcher”;第二个默认:设置的为location参数,location只能是页面,不能是另一个action(可用type=”chain”解决)。

(2)实现方式

从doExecute方法看出,有三个出口(finalLocation为要跳转的地址):

*

pageContext.include(finalLocation);
*

dispatcher.forward(request, response); (dispatcher是根据finalLocation创建的)
*

dispatcher.include(request, response);

而我们知道,forward与include都是转发到context内部的资源。

二、redirect

(1)可以重定向到一个页面,另一个action或一个网址。
Xml代码

1. aaa.jsp
2. bbb.action
3. www.baidu.com

aaa.jsp
bbb.action
www.baidu.com

(2)实现方式:

查看doExecute方法,只有一个出口:

response.sendRedirect(finalLocation);

sendRedirect是重定向,是重新产生一个HTTP请求到服务器,故重定向后其原来所在的action上下文就不可用了。

三、chain

(1)主要用于把相关的几个action连接起来,共同完成一个功能。
Xml代码


step2 //注:是step2而不是step2.action


finish.jsp

(2)实现方式:

查看execute()方法,主要思想如下:

// 根据Action名称finalActionName及要调用的方法finalMethodName来new一个代理对象proxy,并执行之
Java代码

proxy = actionProxyFactory.createActionProxy(finalNamespace,
finalActionName, finalMethodName, extraContext);
proxy.execute();

(3)多个action间数据的传递

主要有两种方式:

1)由于处于chain中的action属于同一个http请求,action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失,共享一个ActionContext,故可以在上下文中获取,在页面上可以直接使用。手动获取的
方法如下:

Java代码

HttpServletRequest request = ServletActionContext.getRequest();
String s=(String)request.getAttribute(“propName”);

2)实现ModelDriven接口

在Step1Action中,加入getModel:
Java代码

1. public Object getModel() {
2. return message;
3. }

public Object getModel() {
return message;
}

在Step2Action中,加入setModel:
Java代码

1. public void setModel(Object o){
2. System.out.println(“message is:”+o);
3. }

public void setModel(Object o){
System.out.println(“message is:”+o);
}

注意,setModel的调用先于execute()方法后于构造方法。


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

一、Struts2.0的创建
1),添加jar包
?? 1, commons-fileupload-1.2.1.jar
?? 2, commons-io-1.3.2.jar
?? 3, commons-logging-1.0.4.jar
?? 4, freemarker-2.3.13.jar
?? 5, ognl-2.6.11.jar
?? 6, struts2-core-2.1.6.jar
?? 7, xwork-2.1.2.jar
注:要与Spring整合,让Spring托管Struts的Action,那么需要添加Struts2.0的
???????? struts2-spring-plugin-2.1.6.jar
2),创建struts.xml,放在src目录下,并添加相应的action
? <?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>??

<include file=”struts-default.xml”/>
<package name=”com.login”? extends=”struts-default”>
?<action name=”login” method=”login”>???
?<result name=”success” type=”redirect”>success.jsp</result>
</action>???
</package>
??? </struts>
3), 在web.xml加入filter,
????????????? (注:用过滤器的原因是:要对传来的各种url进行分类,转向,即过滤。相似的还有servlet
?????????????????????? 而Spring用监听器,listen,当监听到action创建时就注入属性)
<filter>
<filter-name>struts2</filter-name>
<filter-class>
??? org.apache.struts2.dispatcher.FilterDispatcher? //默认就会读取src下的struts.xml
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
4), 做jsp即可


Warning: Undefined array key "HTTP_REFERER" in /www/wwwroot/prod/www.enjoyasp.net/wp-content/plugins/google-highlight/google-hilite.php on line 58

注:要与Spring整合,让Spring托管Struts的Action,那么需要添加Struts2.0的
????? struts2-spring-plugin-2.1.6.jar
注入效果:action的属性对象会自动被Spring创建,在action中只需引用即可

1,添加Spring能力
选中以下及自动关联的类库
String 2.0 Core Libraries
String 2.0 Persistence JDBC Libraries
String 2.0 Web Libraries

2, 在web.xml添加spring监听器
<listener>
<listener-class>
??? org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

注:若不用Spring来为Struts的action进行自动属性注入,那么就不必为spring配置监听器,且也不用导入
struts2-spring-plugin-2.1.6.jar, 直接在代码中进行手动注入:
?????????? ApplicationContext context = new ClassPathXmlApplicationContext( “applicationContext.xml” );
????????? ComeAction comeAction = (ComeAction) context.getBean( “comeAction” );
可看出在web.xml配置的多为struts与servlet,因为它们要拦截页面请求的url进行转向,而配置
?? Spring监听器的原因是为了监听Struts action的建立并为其动态注入属性。

3, 将applicationContext.xml放置在web-inf下, 名字可任意如ac.xml,ab.xml
不过要在web.xml指定??
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/a*.xml </param-value>
</context-param>
注:<context-param>也可不加,默认读取的是/WEB-INF/下的applicationContext.xml,只要?????? applicationContext.xml放置在web-inf下即可。

4, 配置 applicationContext.xml, 创建相应的注入属性
如:loginAction有一个userDao属性,设置了get,set方法,那么在 applicationContext.xml,配置:
??? <bean id=”userDao”>
???? </bean>
注:1,loginAction中的属性名userDao一定要与applicationContext.xml中的bean的id相同!!!!!
这样Spring才能自动注入action的属性。指的是与setUserDao()中userDao相同,变量名不必要相同。
因为在注入时,会根据配置文件中的userDao调用action的setUserDao方法,只要此方法存在即可!!
???? ( 以下2,3可不用配 )
?????? 2, 在struts.properties中设置struts.objectFactory属性值
?????????? struts.objectFactory = spring?
????????? 或者是在XML文件中进行常量配置
?????????? struts.xml???? <constant name=”struts.objectFactory” value=”spring” />?
???? 3, 在struts.properties中指定注入方式。name 按照你的action的属性的名字和Spring里的bean的名字匹配,如果匹配就自动装配。这是缺省的
type 按照你的action的属性的类型,在Spring注册的bean中查找,如果相同就自动装配。这需要你在Spring中仅注册了一个此类型的bean
auto Spring会试图自动监测来找到最好的方法自动装配你的action
constructor Spring会自动装配bean的构造函数的参数
ps :
在action的class指定具体的类,Spring自动完成属性的注入,
在action的class指定bean id, Spring托管整个action生命周期.
1, 属性注入:
(1) struts.xml中配置:
?<action name=”login” method=”login”>???
<result name=”success” type=”redirect”>success.jsp</result>
</action>
(2) applicationContext.xml中配置
?在applicationContext.xml中配置此action的属性,要注意的是bean id 要与action内的属性名相同,不用为???????????????? LoginAction本身定义bean id.
这样Spring就只负责action属性的注入,

2, 所有注入:?? 让spring不仅负责action属性的创建,还负责action本身的创建.
? (1) struts.xml中配置:??
?? struts.xml中配置:
? <action name=”login” method=”login”>???
<result name=”success” type=”redirect”>success.jsp</result>
?</action>
? class为Spring中applicationContext.xml中的bean id
?(2) applicationContext.xml中配置
??? 配置loginAction的bean即可.
所有注入的好处: 结合aop,可完成对action执行具体方法时进行拦截,进行日志记录等.