正在用 SSH(struts2.0+spring3.0+hibernate2.0)開發(fā)一個(gè)項(xiàng)目,但是struts2.0 請(qǐng)求action 后轉(zhuǎn)到的頁面的后綴都成了XXX_XXXX.action (我用的通配符設(shè)置的action).看著很是不習(xí)慣,需要修改!??!
(前臺(tái)的頁面顯示需要數(shù)據(jù)庫中的數(shù)據(jù),所以顯示之前必須訪問數(shù)據(jù)庫??!)
解決方案:
1,開始想用Ajax技術(shù)。來取得數(shù)據(jù)庫的數(shù)據(jù)。但一旦數(shù)據(jù)量過大或過于凌亂,則是個(gè)大問題!
2,由于struts2.0 訪問數(shù)據(jù)庫之后,頁面顯示的url 都是訪問的action 的名字?,F(xiàn)在考慮修改struts2.0的攔截器的攔截后綴。(默認(rèn)為.action,現(xiàn)想修改為.jsp)如此以來,則前臺(tái)的URL 就不會(huì)出現(xiàn)XXX_XXXX.action ,而是XXX.jsp.
查閱資料:得到: 可以在 struts.xml 配置文件中 修改 攔截器的攔截屬性:
修改struts.xml文件,在<struts></struts>中加入下面這句話。
- <constant name="struts.action.extension" value="action,jsp" />
- <constant name="struts.action.extension" value="action,jsp" />
<constant name="struts.action.extension" value="action,jsp" />
如此以來,strtus2.0 就開始 攔截 后綴為 .action 和 .jsp 的請(qǐng)求。
例:如果訪問的頁面為 index.jsp
struts.xml 代碼為
- <?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.custom.i18n.resources"
- value="ApplicationResources" />
- <constant name="struts.i18n.encoding" value="gb2312" />
- <constant name="struts.i18n.reload" value="false"></constant>
- <constant name="struts.action.extension" value="action,jsp" />
- <!-- 設(shè)置上傳文件時(shí)不要Tomcat的臨時(shí)路徑,使用設(shè)置的值 -->
- <constant name="struts.multipart.saveDir" value="/temp" />
- <!-- 設(shè)置上傳文件大小 -->
- <constant name="struts.multipart.maxSize" value="524280" />
- <package name="outlook" extends="struts-default">
- <global-results>
- <!-- 下面定義的結(jié)果對(duì)所有的Action都有效 -->
- <result name="exception">/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
- <exception-mapping exception="java.lang.Exception"
- result="exception" />
- </global-exception-mappings>
- </package>
- <!-- 網(wǎng)站后臺(tái)配置 -->
- <package name="Outlook" extends="struts-default" namespace="/">
- <interceptors>
- <!-- 定義了一個(gè)名為authority的攔截器 -->
- <interceptor name="authority"
- class="org.boss.action.AuthorityInterceptor">
- </interceptor>
- </interceptors>
- <!-- 定義全局Results -->
- <global-results>
- <result name="success">/barhome/success.jsp</result>
- <result name="none">/barhome/doerror.jsp</result>
- <result name="exception">/barhome/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
- <exception-mapping exception="java.lang.Exception"
- result="exception" />
- </global-exception-mappings>
- <action name="index" class="org.boss.action.OutLookAction">
- <result name="goindex">/home.jsp</result>
- </action>
- </package>
- </struts>
- <?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.custom.i18n.resources"
- value="ApplicationResources" />
- <constant name="struts.i18n.encoding" value="gb2312" />
- <constant name="struts.i18n.reload" value="false"></constant>
- <constant name="struts.action.extension" value="action,jsp" />
- <!-- 設(shè)置上傳文件時(shí)不要Tomcat的臨時(shí)路徑,使用設(shè)置的值 -->
- <constant name="struts.multipart.saveDir" value="/temp" />
- <!-- 設(shè)置上傳文件大小 -->
- <constant name="struts.multipart.maxSize" value="524280" />
- <package name="outlook" extends="struts-default">
- <global-results>
- <!-- 下面定義的結(jié)果對(duì)所有的Action都有效 -->
- <result name="exception">/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
- <exception-mapping exception="java.lang.Exception"
- result="exception" />
- </global-exception-mappings>
- </package>
- <!-- 網(wǎng)站后臺(tái)配置 -->
- <package name="Outlook" extends="struts-default" namespace="/">
- <interceptors>
- <!-- 定義了一個(gè)名為authority的攔截器 -->
- <interceptor name="authority"
- class="org.boss.action.AuthorityInterceptor">
- </interceptor>
- </interceptors>
- <!-- 定義全局Results -->
- <global-results>
- <result name="success">/barhome/success.jsp</result>
- <result name="none">/barhome/doerror.jsp</result>
- <result name="exception">/barhome/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
- <exception-mapping exception="java.lang.Exception"
- result="exception" />
- </global-exception-mappings>
- <action name="index" class="org.boss.action.OutLookAction">
- <result name="goindex">/home.jsp</result>
- </action>
- </package>
- </struts>
<?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.custom.i18n.resources"
value="ApplicationResources" />
<constant name="struts.i18n.encoding" value="gb2312" />
<constant name="struts.i18n.reload" value="false"></constant>
<constant name="struts.action.extension" value="action,jsp" />
<!-- 設(shè)置上傳文件時(shí)不要Tomcat的臨時(shí)路徑,使用設(shè)置的值 -->
<constant name="struts.multipart.saveDir" value="/temp" />
<!-- 設(shè)置上傳文件大小 -->
<constant name="struts.multipart.maxSize" value="524280" />
<package name="outlook" extends="struts-default">
<global-results>
<!-- 下面定義的結(jié)果對(duì)所有的Action都有效 -->
<result name="exception">/error.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
<exception-mapping exception="java.lang.Exception"
result="exception" />
</global-exception-mappings>
</package>
<!-- 網(wǎng)站后臺(tái)配置 -->
<package name="Outlook" extends="struts-default" namespace="/">
<interceptors>
<!-- 定義了一個(gè)名為authority的攔截器 -->
<interceptor name="authority"
class="org.boss.action.AuthorityInterceptor">
</interceptor>
</interceptors>
<!-- 定義全局Results -->
<global-results>
<result name="success">/barhome/success.jsp</result>
<result name="none">/barhome/doerror.jsp</result>
<result name="exception">/barhome/error.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 指Action拋出Exception異常時(shí),轉(zhuǎn)入名為exception的結(jié)果。 -->
<exception-mapping exception="java.lang.Exception"
result="exception" />
</global-exception-mappings>
<action name="index" class="org.boss.action.OutLookAction">
<result name="goindex">/home.jsp</result>
</action>
</package>
</struts>
action 的代碼
- public String execute()throws Exception{
- this.getChannel();
-
- return "goindex";
- }
- public String execute()throws Exception{
- this.getChannel();
-
- return "goindex";
- }
public String execute()throws Exception{
this.getChannel();// 網(wǎng)站頻道信息
return "goindex";
}
OK ,問題解決??!現(xiàn)在URL的顯示正常了。??!