??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲一区激情,a天堂中文在线,成年人免费在线视频http://www.aygfsteel.com/fantasyginge/category/5850.htmlzh-cnWed, 28 Feb 2007 07:43:43 GMTWed, 28 Feb 2007 07:43:43 GMT60Extending RequestProcessor to provide customized processorRolehttp://www.aygfsteel.com/fantasyginge/articles/23300.htmlfantasygingefantasygingeSat, 10 Dec 2005 15:51:00 GMThttp://www.aygfsteel.com/fantasyginge/articles/23300.htmlhttp://www.aygfsteel.com/fantasyginge/comments/23300.htmlhttp://www.aygfsteel.com/fantasyginge/articles/23300.html#Feedback0http://www.aygfsteel.com/fantasyginge/comments/commentRss/23300.htmlhttp://www.aygfsteel.com/fantasyginge/services/trackbacks/23300.html
Q一Q?br>   下列是所有文Ӟ(x)
  CheckRequestProcessor  RequestProcessorcd
  UserLoginAction        action
  userLogin.jsp
  successfullyLogin.jsp
  failureLogin.jsp
  manage.jsp
  accessDenied.jsp

Q二Q文件的内容Q?br>
2.1Q?br> struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="userLoginForm" type="cn.edu.scut.www.ginge.form.UserLoginForm" />

  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userLoginForm"
      input="/form/userLogin.jsp"
      name="userLoginForm"
      path="/userLogin"
      scope="request"
      type="cn.edu.scut.www.ginge.action.UserLoginAction">
      <forward name="successfullyLogin" path="/successfullyLogin.jsp" />
      <forward name="failureLogin" path="/failureLogin.jsp" />
    </action>
   
    <action
     path="/accessManagePage"
     type="cn.edu.scut.www.ginge.action.AccessManagePageAction"
     roles="administrator">
      <forward name="success" path="/manage.jsp" />
      <forward name="failure" path="/accessDenied.jsp" />
    </action>

  </action-mappings>

  <controller>
        <set-property  property="processorClass"
        value="cn.edu.scut.www.ginge.CheckRequestProcessor"/>
 </controller>
  <message-resources parameter="cn.edu.scut.www.ginge.ApplicationResources" />
</struts-config>


2.2Q?br> userLogin.jsp的主要内?
 
        <html:form action="/userLogin.do">
            userName : <html:text property="userName"/><html:errors property="userName"/><br/>
            password : <html:password property="password"/><html:errors property="password"/><br/>
            <html:submit/><html:cancel/>

2.3Q?br> successfullyLogin.jsp的主要内容:(x)
<a href="http://localhost:8099/StrutsRoles/accessManagePage.do">manage all members</a>

2.4 manage.jsp的主要内容:(x)
<h1><font color="red">q是理员管理的面</font></h1> <br>

2.5 accessDenied.jsp的主要内容:(x)
 <h1><font color="red">抱歉Q你的权限不够!</font></h1>

2.6Q?br> UserLoginAction.java:


//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.9.210/xslt/JavaClass.xsl

package cn.edu.scut.www.ginge.action;

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

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

import cn.edu.scut.www.ginge.form.UserLoginForm;

/**
 * MyEclipse Struts
 * Creation date: 12-10-2005
 *
 * XDoclet definition:
 * @struts:action path="/userLogin" name="userLoginForm" input="/form/userLogin.jsp" scope="request" validate="true"
 * @struts:action-forward name="success" path="/accessDenied.jsp"
 * @struts:action-forward name="failure" path="/accessDenied.jsp"
 */
public class UserLoginAction extends Action {

    // --------------------------------------------------------- Instance Variables

    // --------------------------------------------------------- Methods

    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {
        UserLoginForm userLoginForm = (UserLoginForm) form;
        if("ginge".equals(userLoginForm.getUserName()) && "ginge".equals(userLoginForm.getPassword()))
            {
               HttpSession session = request.getSession(true);
               session.setAttribute("userName", userLoginForm.getUserName());
               return mapping.findForward("successfullyLogin");
            }
        if("fantasyginge".equals(userLoginForm.getUserName()) && "fantasyginge".equals(userLoginForm.getPassword()))
            {
               HttpSession session = request.getSession(true);
               session.setAttribute("userName", userLoginForm.getUserName());
               return mapping.findForward("successfullyLogin");
            }
        else
            return mapping.findForward("failureLogin");
    }

}

2.7Q?br> AccessManagePageAction.java

//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.9.210/xslt/JavaClass.xsl

package cn.edu.scut.www.ginge.action;

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;

/**
 * MyEclipse Struts
 * Creation date: 12-10-2005
 *
 * XDoclet definition:
 * @struts:action validate="true"
 * @struts:action-forward name="success" path="/authorizedAccess.jsp"
 * @struts:action-forward name="failure" path="/accessDenied.jsp"
 */
public class AccessManagePageAction extends Action {

    // --------------------------------------------------------- Instance Variables

    // --------------------------------------------------------- Methods

    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) {

        //if processRoles passed, then forward the user to the resources he has asked
        return mapping.findForward("success");
    }

}



2.8

q是RequestProcess  process method的默认实玎ͼ(x)

public void process(HttpServletRequest request,
                        HttpServletResponse response)
        throws IOException, ServletException {

        // Wrap multipart requests with a special wrapper
        request = processMultipart(request);

        // Identify the path component we will use to select a mapping
        String path = processPath(request, response);
        if (path == null) {
            return;
        }
       
        if (log.isDebugEnabled()) {
            log.debug("Processing a '" + request.getMethod() +
                      "' for path '" + path + "'");
        }

        // Select a Locale for the current user if requested
        processLocale(request, response);

        // Set the content type and no-caching headers if requested
        processContent(request, response);
        processNoCache(request, response);

        // General purpose preprocessing hook
        if (!processPreprocess(request, response)) {
            return;
        }
       
        this.processCachedMessages(request, response);

        // Identify the mapping for this request
        ActionMapping mapping = processMapping(request, response, path);
        if (mapping == null) {
            return;
        }

        // Check for any role required to perform this action
        if (!processRoles(request, response, mapping)) {
            return;
        }

        // Process any ActionForm bean related to this request
        ActionForm form = processActionForm(request, response, mapping);
        processPopulate(request, response, form, mapping);
        if (!processValidate(request, response, form, mapping)) {
            return;
        }

        // Process a forward or include specified by this mapping
        if (!processForward(request, response, mapping)) {
            return;
        }
       
        if (!processInclude(request, response, mapping)) {
            return;
        }

        // Create or acquire the Action instance to process this request
        Action action = processActionCreate(request, response, mapping);
        if (action == null) {
            return;
        }

        // Call the Action instance itself
        ActionForward forward =
            processActionPerform(request, response,
                                 action, form, mapping);

        // Process the returned ActionForward instance
        processForwardConfig(request, response, forward);

    }


2.9
CheckRequestProcessor  的代码:(x)

package cn.edu.scut.www.ginge;

import java.io.IOException;

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

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.RequestProcessor;

public class CheckRequestProcessor extends RequestProcessor {

    protected boolean processPreprocess(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO Auto-generated method stub
        HttpSession session = request.getSession(false);
        // If user is trying to access login page
        // then don't check
        if (request.getServletPath().equals("/userLogin.do")
                || request.getServletPath().equals("/login.do"))
            return true;
        // Check if userName attribute is there is session.
        // If so, it means user has allready logged in
        if (session != null && session.getAttribute("userName") != null)
            return true;
        else {
            try {
                // If no redirect user to login Page
                request.getRequestDispatcher("/form/userLogin.jsp").forward(request,
                        response);
            } catch (Exception ex) {
            }
        }
        return false;
    }

    protected boolean processRoles(HttpServletRequest request,
            HttpServletResponse response, ActionMapping mapping) throws IOException,
            ServletException {
        // TODO Auto-generated method stub
        String [] roles = mapping.getRoleNames();
        if ((roles == null) || (roles.length < 1)) {
            return (true);
        }
       
        HttpSession session = request.getSession(false);
       
        String userName = null;
        String actualRoles = null;
        if (session != null && session.getAttribute("userName") != null)
        {
            userName = (String)session.getAttribute("userName");
           
            //unidentified user
            if((actualRoles = processUserActualRole(userName)) == null)
            {
                return false;
            }
           
            for(int i = 0; i < roles.length; i ++)
            {
                if (actualRoles.equalsIgnoreCase(roles[i])) {
                    if (log.isDebugEnabled()) {
                        log.debug(" User '" + request.getRemoteUser() +
                            "' has role '" + roles[i] + "', granting access");
                    }
                   
                    return (true);
                }
            }
        }
       
//         The current user is not authorized for this action
        if (log.isDebugEnabled()) {
            log.debug(" User '" + request.getRemoteUser() +
                      "' does not have any required role, denying access");
        }
       
       
        //user's priviledge isn't sufficient
        /*
        response.sendError(
                HttpServletResponse.SC_FORBIDDEN,
                getInternal().getMessage("notAuthorized", mapping.getPath()));
                */
        try {
            // If no redirect user to login Page
            request.getRequestDispatcher((mapping.findForward("failure")).getPath()).forward(request,
                    response);
        } catch (Exception ex) {
        }
       
       
        return (false);
    }

    protected void processContent(HttpServletRequest request,
            HttpServletResponse response) {
        // TODO Auto-generated method stub
        // Check if user is requesting ContactImageAction
        // if yes then set image/gif as content type

        response.setContentType("text/html;charset=gb2312");

    }
   
    protected String processUserActualRole(String userName)
    {
        //or other bussiness login,or access database to get the user information
        if("fantasyginge".equalsIgnoreCase(userName))
        {
            return "administrator";
        }
        else if("ginge".equals(userName))
        {
            return "normalUser";
        }
        else
            return null;
    }


}


E序q行效果Q?br> 用用户名gingedq且讉K已设roles讉K控制的结果:(x)

抱歉Q你的权限不够!


而用fantasygingedq且讉K已设roles讉K控制的结果:(x)

q是理员管理的面




fantasyginge 2005-12-10 23:51 发表评论
]]>
【{载】扩展Strutshttp://www.aygfsteel.com/fantasyginge/articles/23252.htmlfantasygingefantasygingeSat, 10 Dec 2005 04:12:00 GMThttp://www.aygfsteel.com/fantasyginge/articles/23252.htmlhttp://www.aygfsteel.com/fantasyginge/comments/23252.htmlhttp://www.aygfsteel.com/fantasyginge/articles/23252.html#Feedback0http://www.aygfsteel.com/fantasyginge/comments/commentRss/23252.htmlhttp://www.aygfsteel.com/fantasyginge/services/trackbacks/23252.html扩展Struts
发表?cnJava | 发表旉: 2005q十?2? 15:48

作者:(x)Sunil Patil

?/SPAN>

我见q许多项目开发者实现自׃有的MVC框架。这些开发者ƈ不是因ؓ(f)惛_C同于Struts的某些功能,而是q没有意识到怎么L展Struts。通过开发自qMVC框架Q你可以掌控全局Q但同时q也意味着你必M出很大的代h(hun)Q在目计划很紧的情况下也许Ҏ(gu)׃可能实现?BR>
Struts不但功能强大也易于扩展。你可以通过三种方式来扩展Struts:

1.PlugInQ在应用启动或关闭时L行某业务逻辑Q创Z自己的PlugInc?BR>
2.RequestProcessorQ在h处理阶段一个特定点Ʋ执行某业务逻辑Q创Z自己的RequestProcessor。例如:(x)你想l承RequestProcessor来检查用L(fng)录及(qing)在执行每个请求时他是否有权限执行某个动作?BR>
3.ActionServletQ在应用启动或关闭或在请求处理阶D|执行某业务逻辑Q(h)承ActionServletcR但是必M只能在PligIn和RequestProcessor都不能满你的需求时候用?BR>
本文?x)列举一个简单的Struts应用来示范如何用以上三U方式扩展Struts。在本文末尾资源区有每种方式的可下蝲样例源代码。Struts Validation 框架?Tiles 框架是最成功两个的Struts扩展例子?BR>
我是假设读者已l熟(zhn)Struts框架q知道怎样使用它创建简单的应用。如想了(jin)解更多有关Struts的资料请参见资源区?BR>
PlugIn

Ҏ(gu)Struts文Q“PlugIn是一个须在应用启动和关闭旉被通知的模块定制资源或服务配置包”。这是_(d)你可以创Z个类Q它实现PlugIn的接口以便在应用启动和关闭时做你惌的事?BR>
假如创徏?jin)一个web应用Q其中用Hibernate做ؓ(f)持久化机Ӟ当应用一启动Q就需初始化HinernateQ这样在web应用接收到第一个请求时QHibernate已被配置完毕q待命。同时在应用关闭时要关闭Hibernate。跟着以下两步可以实现Hibernate PlugIn的需求?BR>
1.创徏一个实现PlugIn接口的类Q如下:(x)

public class HibernatePlugIn implements PlugIn{
        private String configFile;
        // This method will be called at application shutdown time
        public void destroy() {
                System.out.println("Entering HibernatePlugIn.destroy()");
                //Put hibernate cleanup code here
                System.out.println("Exiting HibernatePlugIn.destroy()");
        }
        //This method will be called at application startup time
        public void init(ActionServlet actionServlet, ModuleConfig config)
                throws ServletException {
                System.out.println("Entering HibernatePlugIn.init()");
                System.out.println("Value of init parameter " +
                                    getConfigFile());
                System.out.println("Exiting HibernatePlugIn.init()");
        }
        public String getConfigFile() {
                return name;
        }
        public void setConfigFile(String string) {
                configFile = string;
        }
}


实现PlugIn接口的类必须是实C下两个方法:(x)
init() 和destroy().。在应用启动时init()被调用,关闭destroy()被调用。Struts允许你传入初始参数给你的PlugInc;Z(jin)传入参数你必dPlugInc里为每个参数创Z个类似JavaBean形式的setterҎ(gu)。在HibernatePlugInc里Q欲传入configFile的名字而不是在应用里将它硬~码q去

2.在struts-condig.xml里面加入以下几行告知Strutsq个新的PlugIn

<struts-config>
        ...
        <!-- Message Resources -->
        <message-resources parameter=
          "sample1.resources.ApplicationResources"/>

        <!-- Declare your plugins -->
        <plug-in className="com.sample.util.HibernatePlugIn">
                <set-property property="configFile"
                   value="/hibernate.cfg.xml"/>
        </plug-in>
</struts-config>

ClassName属性是实现PlugIn接口cȝ全名。ؓ(f)每一个初始化传入PlugIncȝ初始化参数增加一?lt;set-property>元素。在q个例子里,传入config文档的名Uͼ所以增加了(jin)一个config文路径?lt;set-property>元素?BR>
Tiles和Validator框架都是利用PlugInl初始化d配置文g。另外两个你q可以在PlugInc里做的事情是:(x)

假如应用依赖于某配置文gQ那么可以在PlugInc里(g)查其可用性,假如配置文g不可用则抛出ServletException。这导致ActionServlet不可用?BR>
PlugIn接口的init()Ҏ(gu)是你改变ModuleConfigҎ(gu)的最后机?x),ModuleConfigҎ(gu)是描q基于Struts模型?rn)态配|信息的集合。一旦PlugIn被处理完毕,Struts׃(x)ModuleCOnfigȝh?BR>
h是如何被处理?/SPAN>

ActionServlet是Struts框架里唯一一个ServletQ它负责处理所有请求。它无论何时收到一个请求,都会(x)首先试着为现有请求找C个子应用。一旦子应用被找刎ͼ它会(x)为其生成一个RequestProcessor对象Qƈ调用传入HttpServletRequest和HttpServletResponse为参数的process()Ҏ(gu)?BR>
大部分请处理都是在RequestProcessor.process()发生的。Process()Ҏ(gu)是以模板Ҏ(gu)QTemplate MethodQ的设计模式来实现的Q其中有完成request处理的每个步骤的Ҏ(gu)Q所有这些方法都从process()Ҏ(gu)序调用。例如,L当前h的ActionFormcd(g)查当前用h否有权限执行action mapping都有几个单独的方法。这l我们提供了(jin)极大的弹性空间。Struts的RequestProcessorҎ(gu)个请求处理步骤都提供?jin)默认的实现?gu)。这意味着Q你可以重写你感兴趣的方法,而其余剩下的保留默认实现。例如,Struts默认调用request.isUserInRole()(g)查用h否有权限执行当前的ActionMappingQ但如果你需要从数据库中查找Q那么你要做的就是重写processRoles()Ҏ(gu)QƈҎ(gu)用户角色q回true ?false?BR>
首先我们看一下process()Ҏ(gu)的默认实现方式,然后我将解释RequestProcessorc里的每个默认的Ҏ(gu)Q以便你军_要修改请求处理的哪一部分?BR>
public void process(HttpServletRequest request,
                        HttpServletResponse response)
    throws IOException, ServletException {
        // Wrap multipart requests with a special wrapper
        request = processMultipart(request);
        // Identify the path component we will
        // use to select a mapping
        String path = processPath(request, response);
        if (path == null) {
            return;
        }
        if (log.isDebugEnabled()) {
            log.debug("Processing a '" + request.getMethod() +
                      "' for path '" + path + "'");
        }
        // Select a Locale for the current user if requested
        processLocale(request, response);
        // Set the content type and no-caching headers
        // if requested
        processContent(request, response);
        processNoCache(request, response);
        // General purpose preprocessing hook
        if (!processPreprocess(request, response)) {
            return;
       }
        // Identify the mapping for this request
        ActionMapping mapping =
            processMapping(request, response, path);
        if (mapping == null) {
            return;
        }
        // Check for any role required to perform this action
        if (!processRoles(request, response, mapping)) {
            return;
        }
        // Process any ActionForm bean related to this request
        ActionForm form =
            processActionForm(request, response, mapping);
        processPopulate(request, response, form, mapping);
        if (!processValidate(request, response, form, mapping)) {
            return;
        }
        // Process a forward or include specified by this mapping
        if (!processForward(request, response, mapping)) {
            return;
        }
        if (!processInclude(request, response, mapping)) {
            return;
        }
        // Create or acquire the Action instance to
        // process this request
        Action action =
            processActionCreate(request, response, mapping);
        if (action == null) {
            return;
        }
        // Call the Action instance itself
        ActionForward forward =
            processActionPerform(request, response,
                                action, form, mapping);
        // Process the returned ActionForward instance
        processForwardConfig(request, response, forward);
    }



1、processMultipart(): 在这个方法中QStrutsdrequest以找出contentType是否为multipart/form-data。假如是Q则解析q将其打包成一个实现HttpServletRequest的包。当你成生一个放|数据的HTML FORMӞrequest的contentType默认是application/x-www-form-urlencoded。但是如果你的form的inputcd是FILE-type允许用户上蝲文gQ那么你必须把form的contentType改ؓ(f)multipart/form-data。如q样做,你永q不能通过HttpServletRequest的getParameter()来读取用h交的form|你必MInputStream的Ş式读取requestQ然后解析它得到倹{?BR>
2、processPath(): 在这个方法中QStruts读取request的URI以判断用来得到ActionMapping元素的\径?BR>
3、processLocale(): 在这个方法中QStruts得到当前request的LocaleQLocale假如被配|,作为org.apache.struts.action.LOCALE属性的D存入HttpSession。这个方法的附作用是HttpSession?x)被创徏。假如你不想此事发生Q可在struts-config.xml 文g里ControllerConfig的local属性设|ؓ(f)false,如下Q?BR>
<controller>
        <set-property property="locale" value="false"/>
</controller>


4、processContent()Q?/B>通过调用response.setContentType()讄response的contentType。这个方法首先会(x)试着的得到配|在struts-config.xml里的contentType。默认ؓ(f)text/htmlQ重写方法如下:(x)
<controller>
        <set-property property="contentType" value="text/plain"/>
</controller>


5、processNoCache()Q?/B>Strutsؓ(f)每个response的设|以下三个headerQ假如已在struts 的config.xml配|ؓ(f)no-cache?BR>
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 1);


假如你想讄为no-cache headerQ在struts-config.xml中加如以下几?BR>
<controller>
        <set-property property="noCache" value="true"/>
</controller>


6、processPreprocess()Q?/B>q是一个一般意义的预处理hookQ其可被子类重写。在RequestProcessor里的实现什么都没有做,Lq回true。如此方法返回false?x)中断请求处理?BR>
7、processMapping():q个Ҏ(gu)?x)利用path信息扑ֈActionMapping对象。ActionMapping对象在struts-config.xml file文g里表CZؓ(f)<action>
<action path="/newcontact" type="com.sample.NewContactAction"
        name="newContactForm" scope="request">
        <forward name="sucess" path="/sucessPage.do"/>
        <forward name="failure" path="/failurePage.do"/>
</action>


ActionMapping元素包含?jin)如Actioncȝ名称?qing)在h中用到的ActionForm的信息,另外q有配置在当前ActionMapping的里的ActionForwards信息?BR>
8、processRoles(): Struts的web 应用安全提供?jin)一个认证机制。这是_(d)一旦用L(fng)录到容器QStruts的processRoles()Ҏ(gu)通过调用request.isUserInRole()可以(g)查他是否有权限执行给定的ActionMapping?BR>        <action path="/addUser" roles="administrator"/>

假如你有一个AddUserActionQ限制只有administrator权限的用h能新d用户。你所要做的就是在AddUserAction 的action元素里添加一个gؓ(f)administrator的role属性?BR>
9、processActionForm()Q?/B>每个ActionMapping都有一个与它关联的ActionFormcRstruts在处理ActionMappingӞ他会(x)?lt;action>里name属性找到相关的ActionFormcȝ倹{?BR>
<form-bean name="newContactForm" 
           type="org.apache.struts.action.DynaActionForm">
                <form-property name="firstName"
                          type="java.lang.String"/>
                <form-property name="lastName"
                          type="java.lang.String"/>
</form-bean>


在这个例子里Q首先会(x)(g)查org.apache.struts.action.DynaActionFormcȝ对象是否在request 范围内。如是,则用它Q否则创Z个新的对象ƈ在request范围内设|它?BR>
10、processPopulate():Q?/B>在这个方法里QStruts匹配的request parameters值填入ActionFormcȝ实例变量中?BR>
11、processValidate()Q?/B>Struts调用ActionForm的validate()Ҏ(gu)。假如validate()q回ActionErrorsQStruts用戯{到由<action>里的input属性标C的面?BR>
12、processForward() and processInclude()Q?/B>在这两个Ҏ(gu)里,Struts(g)?lt;action>元素的forward和include属性的|假如有配|,则把forward和include h攑֜配置的页面内?BR>
<action forward="/Login.jsp" path="/loginInput"/>
        <action include="/Login.jsp" path="/loginInput"/>


你可以从他们的名字看出其不同之处。processForward()调用RequestDispatcher.forward(),QprocessInclude()调用RequestDispatcher.include()。假如你同时配置?jin)orward 和include 属性,StrutsM(x)调用forwardQ因为forward,是首先被处理的?BR>
13、processActionCreate()Q?/B>q个Ҏ(gu)?lt;action>的type属性得到ActioncdQƈ创徏q回它的实例。在q里例子中struts创Z个com.sample.NewContactActioncȝ实例?BR>
14、processActionPerform()Q?/B>q个Ҏ(gu)调用Action cȝexecute()Ҏ(gu)Q其中有你写入的业务逻辑?BR>
15、processForwardConfig()Q?/B>Actioncȝexecute()会(x)q回一个ActionForwardcd的对象,指出哪一面展C给用户。因此Strutsؓ(f)q个面创徏RequestDispatchetQ然后再调用RequestDispatcher.forward()Ҏ(gu)?BR>
以上列出的方法解释了(jin)RequestProcessor在请求处理的每步默认实现?qing)各个步骤执行的序。正如你所见,RequestProcessor很有Ҏ(gu),它允怽通过讄<controller>里的属性来配置它。例如,假如你的应用生成XML内容而不是HTMLQ你可以通过讄controller的某个属性来通知Struts?BR>
创徏你自qRequestProcessor

从以上内Ҏ(gu)们已l明白了(jin)RequestProcessor的默认实现是怎样工作的,现在我将通过创徏你自qRequestProcessor.展示一个怎样自定义RequestProcessor的例子。ؓ(f)?jin)演C创Z个自定义RequestProcessorQ我修改例子实C下连个业务需求:(x)

我们要创Z个ContactImageActionc,它将生成images而不是一般的HTMl面

在处理这个请求之前,通过(g)查session里的userName属性来认用户是否d。假如此属性没有被扑ֈQ则用戯{到登录页面?BR>

分两步来实现以上q个业务需求?BR>创徏你自qCustomRequestProcessorc,它将l承RequestProcessorc,如下Q?BR>
public class CustomRequestProcessor
    extends RequestProcessor {
        protected boolean processPreprocess (
            HttpServletRequest request,
            HttpServletResponse response) {
            HttpSession session = request.getSession(false);
        //If user is trying to access login page
        // then don't check
        if( request.getServletPath().equals("/loginInput.do")
            || request.getServletPath().equals("/login.do") )
            return true;
        //Check if userName attribute is there is session.
        //If so, it means user has allready logged in
        if( session != null &&
        session.getAttribute("userName") != null)
            return true;
        else{
            try{
                //If no redirect user to login Page
                request.getRequestDispatcher
                    ("/Login.jsp").forward(request,response);
            }catch(Exception ex){
            }
        }
        return false;
    }

    protected void processContent(HttpServletRequest request,
                HttpServletResponse response) {
            //Check if user is requesting ContactImageAction
            // if yes then set image/gif as content type
            if( request.getServletPath().equals("/contactimage.do")){
                response.setContentType("image/gif");
                return;
            }
        super.processContent(request, response);
    }
}


在CustomRequestProcessor cȝprocessPreprocessҎ(gu)里,(g)查session的userName属性,假如没有扑ֈQ将用户转到d面?BR>
对于产生images作ؓ(f)ContactImageActioncȝ输出Q必要重写processContentҎ(gu)。首先检查其request是否h/contactimage路径Q如是则讄contentType为image/gifQ否则ؓ(f)text/html?BR>
加入以下几行代码到sruts-config.xml文g里的<action-mapping>后面Q告知Struts QCustomRequestProcessor应该被用作RequestProcessorc?BR>
<controller>
        <set-property  property="processorClass"
        value="com.sample.util.CustomRequestProcessor"/>
</controller>


h意,假如你只是很生成contentType不是text/html输出的Actionc,重写processContent()没有问题。如不是q种情况Q你必须创徏一个Struts子系l来处理生成image  Action的请求ƈ讄contentType为image/gif

Title框架使用自己的RequestProcessor来装饰Struts生成的输出?BR>
ActionServlet

假如你仔l研IStruts web应用的web.xml文gQ它看上dq样Q?BR>
<web-app >
        <servlet>
            <servlet-name>action=</servlet-name>
            <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
            <!-- All your init-params go here-->
        </servlet>
        <servlet-mapping>
            <servlet-name>action</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
</web-app >


q就是说QActionServlet负责处理所有发向Struts的请求。你可以创徏ActionServlet的一个子c,假如你想在应用启动和关闭时或每次h时做某些事情。但是你必须在(h)承ActionServletcd创徏PlugIn ?RequestProcessor。在Servlet 1.1前,Title框架是基于(h)承ActionServletcL装饰一个生成的response。但?.1开始,׃用TilesRequestProcessorcR?BR>
l论

开发你自己的MVC模型是一个很大的军_(j)——你必须考虑开发和l护代码的时间和资源。Struts是一个功能强大且E_的框Ӟ你可以修改它以其满你大部分的业务需求?BR>
另一斚wQ也不要L地决定扩展Struts。假如你在RequestProcessor里放入一些低效率的代码,q些代码在每次h时执行ƈ大大地降低整个应用的效率。当然L创徏你自qMVC框架比扩展Struts更好的情c(din)?BR>


fantasyginge 2005-12-10 12:12 发表评论
]]>
վ֩ģ壺 | Ȫ| ߺ| Ǭ| ʯɽ| | | | ڳ| ɽ| 㽭ʡ| ½| | | ī| | | ҵ| | | | ˮ| | Ӽ| | Ӻ| | | Ҫ| ǰ| ʡ| | | | | | | ɽ| | ͸| ǭ|