Skip to content

Instantly share code, notes, and snippets.

@Gaubee
Last active December 26, 2015 10:29
Show Gist options
  • Select an option

  • Save Gaubee/7137302 to your computer and use it in GitHub Desktop.

Select an option

Save Gaubee/7137302 to your computer and use it in GitHub Desktop.
Struts2 开发入门及其相关的原理与配置
@Gaubee
Copy link
Author

Gaubee commented Oct 27, 2013

Struts配置:
通过web.xml来声明拦截器

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

注意:这里*.action拦截后,只剩下 *。
则.action后缀的访问都会被拦截器拦截进入 struts.xml文件进一步查找。

    <package name="gaubee" namespace="/" extends="struts-default">
        <action name="gaubeelogin" class="com.gaubee.struts2.LoginAction">
            <result name="input">/login.jsp</result>
            <result name="success">/result.jsp</result>
            <result name="error">/result.jsp</result>
        </action>
    </package>

namespace是用来进一步过滤,好像是struts2.1+才需要这个配置。
就是你网址后的路径,如果namespace="/example" ,则需要多一层/example/gaubeelogin.action
紧接着是action,这里的name不能写成="gaubeelogin.action",因为后缀已经被过滤掉了
class要写对,不然整个网址跑起来后直接报错404,不论哪个页面

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment