【转】修改struts2 显示的URL 后缀 .action 为 .jsp
正在用 SSH(struts2.0+spring3.0+hibernate2.0)开发一个项目,但是struts2.0 请求action 后转到的页面的后缀都成了XXX_XXXX.action (我用的通配符设置的action).看着很是不习惯,需要修改!!!
(前台的页面显示需要数据库中的数据,所以显示之前必须访问数据库!!)
解决方案:
1,开始想用Ajax技术。来取得数据库的数据。但一旦数据量过大或过于凌乱,则是个大问题!
2,由于struts2.0 访问数据库之后,页面显示的url 都是访问的action 的名字。现在考虑修改struts2.0的拦截器的拦截后缀。(默认为.action,现想修改为.jsp)如此以来,则前台的URL 就不会出现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" />
如此以来,strtus2.0 就开始 拦截 后缀为 .action 和 .jsp 的请求。
例:如果访问的页面为 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" />
- <!-- 设置上传文件时不要Tomcat的临时路径,使用设置的值 -->
- <constant name="struts.multipart.saveDir" value="/temp" />
- <!-- 设置上传文件大小 -->
- <constant name="struts.multipart.maxSize" value="524280" />
- <package name="outlook" extends="struts-default">
- <global-results>
- <!-- 下面定义的结果对所有的Action都有效 -->
- <result name="exception">/error.jsp</result>
- </global-results>
- <global-exception-mappings>
- <!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
- <exception-mapping exception="java.lang.Exception"
- result="exception" />
- </global-exception-mappings>
- </package>
- <!-- 网站后台配置 -->
- <package name="Outlook" extends="struts-default" namespace="/">
- <interceptors>
- <!-- 定义了一个名为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异常时,转入名为exception的结果。 -->
- <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" />
<!-- 设置上传文件时不要Tomcat的临时路径,使用设置的值 -->
<constant name="struts.multipart.saveDir" value="/temp" />
<!-- 设置上传文件大小 -->
<constant name="struts.multipart.maxSize" value="524280" />
<package name="outlook" extends="struts-default">
<global-results>
<!-- 下面定义的结果对所有的Action都有效 -->
<result name="exception">/error.jsp</result>
</global-results>
<global-exception-mappings>
<!-- 指Action抛出Exception异常时,转入名为exception的结果。 -->
<exception-mapping exception="java.lang.Exception"
result="exception" />
</global-exception-mappings>
</package>
<!-- 网站后台配置 -->
<package name="Outlook" extends="struts-default" namespace="/">
<interceptors>
<!-- 定义了一个名为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异常时,转入名为exception的结果。 -->
<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";
}
OK ,问题解决!!现在URL的显示正常了。!!