随笔-21  评论-29  文章-0  trackbacks-0
 

StrutsMVC组件
        组件包括:ActionServlet Action Classes Action Mapping(此处包括ActionForward)、ActionForm Bean
Struts中的MVC

        1
、模型(Model):本质上来说在StrutsModel是一个商业逻辑类,开发者实现商业逻辑。
        2
、视图(View):View是由与控制器Servlet配合工作的一整套JSP定制标签库构成,利用他们可以快速建立应用系统的界面。
        3
、控制器(Controller):前端控制器是一个Servlet。它将客户端请求转发到相应的后端控制器ActionForm类。
ActionServlet(中心控制器)
——参看APIDOC
      
定义:继承自javax.servlet.http.HttpServlet类,是中心控制器(总控制器)。它提供一个中心位置来处理全部的终端请求。
      
作用:接受请求、填充数据、派发请求、响应用户  
    配置:在web配置文件web.xml中声明。

<servlet>
       
<servlet-name>action</servlet-name>
       
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
       
<init-param>
           
<param-name>config</param-name>
           
<param-value>/WEB-INF/struts-config.xml</param-value>
       
</init-param>
       
<load-on-startup>0</load-on-startup>
    
</servlet>

    
<servlet-mapping>
       
<servlet-name>action</servlet-name>  
       
<url-pattern>*.do</url-pattern>
    
</servlet-mapping>

Action 
  

APIDOC:An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.
  问题:Action是什么时候初始化的?服务启动的时候,还是接收到用户第一次请求的时候?
AddStudentAction中加入以下代码(加入其构造方法)

public AddStudentAction(){
        System.out.println(
"in AddStudentAction constructor");
    }

重新部署后观察页面和控制器


以上证明:Action发出该Action请求,不是在读取配置时初始化的!
再打开另外一个浏览器,新建一个会话,发现该语句只打印一次,即证明Action只初始化一次!

APIDOC:Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:  

  • Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
  • Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary.

注意:Action的线程安全性,不能多个请求共享一个Action实例。
怎样实现Action的安全性编程?
   注意不要用实例变量或者类变量共享只是针对某个请求的数据
   注意资源操作的同步性。
应用:可以统计一个Action访问次数。(设计一个实例变量count 其在调用时加1)
新建一个CountAction类,其继承自Action类


在struts-config.xml中注册  即加入如下代码
<action path="/countAction" type="cn.itcast.CountAction"></action>
编辑CountAction代码 如下

package cn.itcast;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class CountAction extends Action {
    
    
private Integer count = 0 ;
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws Exception {
        
        
synchronized (count){
        count
++ ;
        }

        PrintWriter out 
= response.getWriter();
        out.println(
"count="+count);        
        
return null ;
    }


}

重新部署应用 http://localhost:8080/strutstest//countAction.do 实现了计数功能





有n个请求就会执行n个对应的execute方法,但这n个execute方法同时操作count变量,要注意线程安全!

posted on 2009-05-02 22:14 特立独行 阅读(244) 评论(0)  编辑  收藏 所属分类: Struts框架

只有注册用户登录后才能发表评论。


网站导航: