`
feicer
  • 浏览: 133162 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Struts2 的 struts.xml 配置中 namespace 的使用

阅读更多
Struts2 的 struts.xml 中是分 package 配置的,可以为 package 设置 namespace 属性,如

<package namespace="/secure"   ....>
    ......
</package>


如果没有指定 namespace 属性,默认 namespace 是 ""。使用 namespace 可以方便于按不同目的规划对应用的访问规则。比如不同 namespace 下配置了不同的拦截器就可以实现权限的控制,如 "/secure" 下已登陆用户才能访问,"/public" 下可公开访问的。

配置了 namespace 直接就是反应在访问 URL 上,例如 namespace="/secure"  name="test" 的 action

 <package namespace="/secure"   ....>
       <action name="test"  ....
</package>


访问它的 URL 就是 http://ip:port/context/secure/test.action,那如果在 namespace "/secure" 下没有 test action 会出现什么情况呢?Struts 还会尝试在默认 namespace,即 "" 下找 test。

再举个例子,URL 是 http://ip:port/context/some/path/test.action 时,如果在 "/some/path" namespace 下找不到 test action,也是到 "" (default namespace) 下找 test action,但不会去 "/some" 下找的。

用标签
<s:url value="/secure/test.action"/>
  对应页面源文件是 /context/secure/test.action

稍有麻的就是
<s:form action="/secure/test.action" .... 
对应的源文件是
<form action="/context/secure/test.action" ...


但是后台会有警告:

警告: No configuration found for the specified action: '/secure/test.action' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

Struts2 把 action 属性值当整一个 Action Name 了,但这也不影响使用,这个 URL 正好能与 (package namespace) + (action name) 合上拍。

但是对于使用了动态方法调用(struts.enable.DynamicMethodInvocation = true)就没这么幸运了。很容易想当然的

<s:form action="/secure/test!update.action" ....
  生成的 HTML 源文件却是 action="/TestStruts2/om/test"

同时后台的警告信息是:

警告: No configuration found for the specified action: '/secure/test' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

很显然对于这个 action="/TestStruts2/om/test",提交时是会得到 HTTP Status 404 - /context/secure/test  错误。

正确的用法是 <s:action...> 也有一个 namespace 属性,对了,就是

<s:form namespace="/secure" action="test!login">
  生成的 HTML 源文件是:
 <form action="/TestStruts2/om/test!login.action" ....>

我们要的就是这个。

如果不配置 namespace 属性,我们能不能在访问 action 时也用上目录层次呢?可以,那是在 struts1 习惯的做法,配置 <action name="secure/test" ....> name 中使用斜杠,但在 Struts2 中 Action Name 中使用斜杠需要设置

struts.enable.SlashesInActionNames=true                      默认为 false

可是 Struts2 大概不赞同这种做法,力挺 namespace 的作用。

对于上面使用了斜框的 Action Name,<s:form 中的写法要用

<s:form action="secure/test">  
               生成 HTML 源文件:<form action="/context/secure/test.action" .....

<s:form action="secure/test!update"> 
           生成 HTML 源文件:<form action="/context/secure/test!login.action" .....


--------------------------------------------------------------------------------

上面的 action 后加不加 .action 无所谓,只是要保证 <s:form>  的 action 属性一定要与 struts.xml 中的 <action> 的 name 匹配上,如果你自作多情的在前面加个斜杠,如写成了

<s:form action="/secure/test!update">
<s:form action="/secure/test">
  或者
<s:form action="/secure/test!update.action">   
生成的 HTML 源文件就都成了:
<form action="/context/secure/test" .....


这也是从 Struts1 带来的弊病,因为 Struts1 中 <html:form> action 属性对应的是 <action> 的 path,而 Struts2 中 <s:form> 的 action 属性对应的是 <action> 的 name;name 要完全匹配,path 可以加些层次。

分享到:
评论

相关推荐

    Struts2的struts.xml配置详细介绍

    配置常量,可以改变Struts 2框架的一些行为 name属性表示常量名称,value属性表示常量值 package元素: 包的作用:简化维护工作,提高重用性 包可以“继承”已定义的包,并可以添加自己包的配置 name属性为必须去且...

    一个struts2的例子:彻底解决STRUTS2 错误There is no Action mapped for namespace / and action name login

    再声讨下struts2,为什么要在框架里面加载一个死的struts.xml,难得不能想struts1 一样的在web.xml里面配置吗? 看网上都催struts2比struts1 整的整的好,我看就这个问题,完全可以算struts2是个垃圾 我现在把完整...

    struts2配置2.5版

    拦截器:web.xml 配置拦截器&lt;filter&gt; struts2.5的filter-class 与struts2.5以前版本有所不同 &lt;!-- 浏览器访问 http://localhost:8080/MyWeb/helloworld --&gt; --&gt; &lt;?xml version="1.0" encoding=...

    ssh整合配置文档

    Struts2.xml配置文档 &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt; ...

    web页面模块化异步渲染struts-gpipe.zip

    2,struts.xml配置 name="gweb.groovy.dir" value="biz" /&gt;  name="gweb" namespace="/index" extends="gpipe-default"&gt;   name="index" class="com.gweb.front.action.Index"&gt;   name="success" type...

    struts-2.3.4.1所需的jar文件

    接触新版本出了问题后,解决了把jar文件上传上来供大家分享, 一共9个jar文件,我试过没问题 ...&lt;package name="default" namespace="/" extends="struts-default"&gt; /helloWorld.jsp &lt;/struts&gt;

    struts2课件

    struts2课件 很好的struts2当输入login.jsp访问jsp页面填写完相关信息并提交给login.action时,它会首先被在web.xml中配置的过滤器监听到,过滤器会去查找strust.xml文件,并结合namespace查找名为login的action,...

    解决struts2下载异常的jar包 struts2-sunspoter-stream-1.0.jar

    在struts2中使用result里type="stream"的结果类型时,可以实现文件的下载管理,使用时也是比较顺畅,但是当在“下载提示窗口”中点击“取消按钮”时,总是报出“java.lang.IllegalStateException”异常,异常内容...

    struts2示例程序

    struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt; ...

    struts2.0扫盲文档

    struts2.0扫盲文档2加入struts.xml配置文件(自己建) &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    Struts2+Spring3+MyBatis3完整实例

    网上的东西好大多都不能直接用,自己结合网上资料做了一个Struts2+Spring3+MyBatis3的测试工程,JUnit测试用例和WEB服务。 内涵完整jar包,解压直接可用,包括一个表文件。 Eclipse3.2+Tomcat/5.5+jdk1.5.0_17 - ...

    Struts升级到Struts2.3.35

    北京时间8月22日13时,Apache官方发布通告公布了Struts2中一个远程代码执行漏洞(cve-2018-11776)。该漏洞可能在两种情况下被触发,第一,当没有为底层xml配置中定义的结果设置namespace 值,并且其上层动作集配置...

    jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图

    -- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --&gt; &lt;include file="struts-default.xml"&gt; &lt;!-- package提供了将多个Action组织为一个模块的方式 ...

    struts2注解详细说明

    需要在你项目的struts.xml中添加如下配置    name="struts.convention.action.suffix" value="Controller"/&gt;   name="struts.convention.action.mapAllMatches" value="true"/&gt;   name="struts.convention....

    Struts2 2.3.16_doc

    This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the ...

    struts项目搭建

    Struts作为MVC 2的Web框架,自推出以来不断受到开发者的追捧,得到用广泛的应用。作为最成功的Web框架,Struts自然拥有众多的优点:  MVC 2模型的使用  功能齐全的标志库(Tag Library)  开放源代码 好了...

    struts2实例 学生信息管理系统

    struts2实现的学生信息管理系统 &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    最新Struts2+jq+ajax+json 学会总要4步‵‵超级简单,里面包含实例

    2. 配置struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    搭建好的一个struts2环境

    为了方便初学者使用Struts2,我配置了一个Struts2空项目,可以直接使用Struts2,IDE使用的是MyEclipse6.5 使用方法: 1.在包com.test.web.action添加类,比如Test1Action.java,该类需要继承ActionSupport 2.在Web...

Global site tag (gtag.js) - Google Analytics