struts2 一些细微技术

1.DefaultAction,一般用在请求不存在时,默认跳转到defaultAction,例如主页

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

<struts>
    <constant name="struts.devMode" value="true" />
    
    <package name="default" namespace="/" extends="struts-default">
    	<default-action-ref name="index"></default-action-ref>
    	<action name="index">
    		<result>/default.jsp</result>
    	</action>
    </package>
</struts>

 

2.模块包含

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

<struts>
    <constant name="struts.devMode" value="true" />
    <include file="login.xml" />
</struts>

 

3.通配符配置  {1}{2}指第几个*, 使用这种方法时约定要做得很好,记住一句话,约定优于配置

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

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="actions" extends="struts-default" namespace="/actions">
        <action name="Student*" class="com.awf.StudentAction" method="{1}">
            <result>/Student{1}_success.jsp</result>
        </action>
        
        <action name="*_*" class="com.awf.action.{1}Action" method="{2}">
            <result>/{1}_{2}_success.jsp</result>
         </action>
    </package>
</struts>

 

4.namespace ,下面列出了三种方式,第一种是"/one"表示访问该package下的action前面加"/one/"例: /one/index

   第二种:"",表示只要最后面是/xx,它就会去访问xx那个action,不过它的优先权最低,如里前面是这样的/one/index,那  么它会去访问刚才说的第一种,如果是这样的/abc/index  刚好没有abc这个namepackage,那么才会去访问第二种.

第三种:"/",不用多说,只要明白它们的优先级就可以了.

 

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

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="front" extends="struts-default" namespace="/one">
        <action name="index">
            <result>/Namespace.jsp</result>
        </action>
    </package>
   
     <package name="main" extends="struts-default" namespace="">
        <action name="index">
            <result>/Namespace.jsp</result>
        </action>
    </package>
<package name="default" namespace="/" extends="struts-default">
   </package>
</struts>

 

 

 

 

5.global-results 全局result,不同package访问可以使用extends

 

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

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
    	
    	<global-results>
    		<result name="mainpage">/main.jsp</result>
    	</global-results>
    	
    	<action name="index">
    		<result>/index.jsp</result>
    	</action>
    	
	    <action name="user" class="com.awf.user.action.UserAction">
	    	<result>/user_success.jsp</result>
	    	<result name="error">/user_error.jsp</result>
	    </action>	    
    </package>
    
    <package name="admin" namespace="/admin" extends="user">
    	<action name="admin" class="com.awf.user.action.AdminAction">
    		<result>/admin.jsp</result>
    	</action>
    </package>
</struts>

 

 

 

 

 6.动态结果集,${r}是ognl对xml的操作 r在action中有get/set方法,这个值实际上是ValueStack,Value Stack Contents里存放的

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

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.awf.struts2.user.action.UserAction">
	    	<result>${r}</result>
	    </action>	    
    </package>
    	
</struts>

 

 

 

7.带参结果集,由于result type=redirect 所以在jsp页面无法用Value Stack Contents,但是可以用Stack Context里的parameters,例:<s:property value="#parameters.t"/> ;Value Stack Contents访问不用#,Stack Context访问要用#key,它们各自包含量的变量有哪些可以在jsp页面使用<s:debug></s:debug>标签

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

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="user" namespace="/user" extends="struts-default">
    	
	    <action name="user" class="com.awf.struts2.user.action.UserAction">
	    	<result type="redirect">/user_success.jsp?t=${type}</result>
	    </action>	    
    </package>
    	
</struts>

 

你可能感兴趣的:(apache,jsp,xml,struts)