Struts2学习---namespace,file模块包含,默认action

我们上一节已经将action基本的配置和使用讲了,接下来我们讲以下struts一些小知识点: 

namespac: 
上一节学习action的时候我们访问我们jsp文件时候使用的: 

http://localhost:8080/testStruts2/hello 
这个路径,有同学就会问,为啥只能用这个路径, 
其实我们也可以用: 
http://localhost:8080/testStruts2/hello.action

这两种是默认的方法,但是同样我们也可以自定义。

<package name="default" namespace="/" extends="struts-default">
        <action name="hello">
            <result>
              /Hello.jsp
            </result>
        </action>
    </package

这是我们上一节的struts.xml配置文件,我们的namespace是一个“/”,不含有其他东西,如果namespace为空或者为”/”,我们来尝试一下:
http://localhost:8080/testStruts2/dd/ddd/hello
这样也可以进行访问。


所以我们就知道了为空或者“/”是一种默认路径,当项目中没有我们指定的路径的时候(dd/ddd/hello),我们这个namespace为空或者为“/”的这个action便承担起了默认访问的作用。

同时我们namespace也可以填写其他的东西: 
例如:namespace=“/index”,然后我们想要访问Hello.jsp的时候就需要:http://localhost:8080/testStruts2/index/hello 
前面要加上一个index。

<constant name="struts.devMode" value="true" />
//顺带讲一下这个,这个是将struts设置为开发者模式,这样修改项目过后就不需要重启服务器了(当然修改过后还是需要保存一下的)

file模块包含: 
我们的struts.xml主配置文件中可以包含其他的struts配置文件(名字不能与struts.xml相同,里面的格式内容相同) 
具体很简单:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.devMode" value="true" />
    <inculde file="xxx.xml"/>
</struts>    

只要将想要包含的配置文件放入<inculde file="xxx.xml"/>中就好了。

默认action: 
当我们访问页面时候,当你访问的页面不存在的时候,会出现错误,所以我们可以为struts设置一个默认页面,当没有用户想访问的页面的时候,我们为用户默认返回一个页面。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
       <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
     <default-action-ref name="index"/>
        <action name="index" class="testStruts1.userAction">
            <result name="success">
              /default.jsp
            </result>
        </action>
    </package>
</struts>    

只要在package里面加上 <default-action-ref name="index"/>就可以让当用户访问页面不存在的时候,访问我们的默认页面。

版权声明:本文为博主原创文章,如需转载请表明出处。 https://blog.csdn.net/qq_39266910/article/details/78485972

原文地址:https://www.cnblogs.com/chengshun/p/9774433.html