JavaScud | Forum | JIRA | Blog |
  Dashboard > SpringSide > Home > StrutsDistill
  SpringSide Log In View a printable version of the current page.  
  StrutsDistill
Added by calvin, last edited by calvin on Oct 12, 2006  (view change)
Labels: 
(None)

Struts 要点

作者:calvin, guangnian0412

文档目录:

  1. 不在struts-config.xml配置jsp路径,直接在代码里跳转。
  2. 不走jsp,直接输出字符串。
  3. Spring与Struts的整合。
  4. 输入校验与消息显示。

1. 不在struts-config.xml配置jsp路径,直接在代码里跳转

return new ActionForward("/foo/bar.jsp");
or
return new ActionForward("/foo/bar.jsp",true);

2. 不走jsp,直接输出字符串

ActionForward execute(....){
    try {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(text);
    } catch (IOException e) {
            log.error(e);
    }
    return null;
}

3. Spring与Struts的整合

3.1 在struts-config.xml中加入ContextLoaderPlugIn

ContextLoaderPlunIn是Struts 1.1+ 的插件,用来为 Struts 的 ActionServlet 加载 Spring Context文件。这个Context引用 WebApplicationContext (由 ContextLoaderListener 加载) 作为它的父Context。默认的context文件是映射的 ActionServlet 的名字,加上 -servlet.xml后缀。 如果 ActionServlet 在 web.xml 里面的定义是 action, 那么默认的文件就是 /WEB-INF/action-servlet.xml

格式如下:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>

 3.2 在Struts-config.xml中加入DelegatingRequestProcessor

 用Spring的DelegatingRequestProcessor重载Struts 默认的 RequestProcessor。这样当收到一个针对Action的请求时,DelegatingRequestProcessor会自动从Spring Context中查找对应的Action Bean。

格式如下:

<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

 3.3 建立struts-config.xml与action-servlet.xml中Action与Bean的对应关系

 连接 struts-config.xmlaction-servlet.xml 中的 Action与Bean的桥梁是action 的"path"和 bean 的"name"。如果你在 struts-config.xml 文件中有如下配置:<action path="/users" .../>
 你必须在 action-servlet.xml 中将 Action bean 的名字定义为 "/users":<bean name="/users" class="..."/>
设定完以后,action的type属性可以省略不写,因为所有的Action都在Spring Context中寻找。注意这里bean用的是name属性,而不是id,因为"/"在id属性中是非法字符。

  如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为<action path="/user"/> ,而且它的 module 前缀为"admin", 那么它应该对应名为  <bean name="/admin/user"/>的bean。

4.输入校验与消息显示

      校验依旧比较麻烦,使用commons-validator 1.3版,使用struts 1.3.5带的validator-rules.xml。可选的validator 以 validator-rules.xml 中的为准。

      注意validaton.xml的头声明需改为1.3

<!DOCTYPE form-validation PUBLIC
        "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
        "http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">

      编写validation.xml,如

<form name="userForm">
            <field property="name" depends="required">
                <arg0 key="user.name"/>
            </field>
</form>

并在resources/i18n/messages.properties 里加入需要的message,如user.name = User Name

     struts-config.xml 加入

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

   客户端:

   在每个form.jsp的form 加入onsubmit="return validateUserForm(this)", 其中UserForm是validation.xml中的form名

    在jsp最后加入

<html:javascript formName="userForm" staticJavascript="false" cdata="false"/>
<script type="text/javascript" src="<c:url value="/scripts/validator.jsp"/>"></script>

  服务端加入:

ActionMessages errors = form.validate(mapping, request);
        if (!errors.isEmpty()) {
            saveErrors(request, errors);
            return mapping.findForward("edit");
        }

   服务端出错信息的显示:

   信息参见message.jsp,可显示信息或错误。

   在希望显示信息的地方加入<%@ include file="/commons/messages.jsp" %>

   如果希望同时在列里面显示出错,加入<span class="fieldError"><html:errors property="user.name"/></span>

Site running on a free Atlassian Confluence Open Source Project License granted to WebWork China. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.3 Build:#808 May 29, 2007) - Bug/feature request - Contact Administrators