Struts2需要注意事项集锦

这些问题是本人开发中遇到的,如果有不同意见的,欢迎指出,大家交流学习

1.写struts2的事情时,在return中,切记SUCCESS等是在Action中定义的,它是一个常量,如果写成了"SUCCESS"这个字符串字面值。如果写成了字符串字面值,在需要在<resutl name="你的字符串字面值">xxx.jsp or xxx.html</result>

注意,这是大小写敏感的哦。

2.在web.xml中,最新版(截至现在是struts-2.2.3.1)中标准的filter写法如下:(前提是你的web.xml中的格式一定要正确)

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <display-name>Struts Blank</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

3.网上很多同志说把struts.xml放到src文件夹下,但这是eclipse中的情况,如果直接放到tomcat的webapps中deploy时,默认的struts.xml应该放在webapp/WEB-INF/classes/ 文件夹下,即:webapp/WEB-INF/classes/struts.xml。

4.关于<package></package>中的name属性,可能有些同志会误解这个属性,以为它的值就是我们java的包名,其实这个名字是可以任意起的,只要不和其他的package中的name相同就可以了。它唯一的作用就是告诉开发人员这个包的唯一名字。

5.<action></action>中的name属性的值要与表单中的action中的一致。如果你的<action></action>中name的值为"hello",则在表单的应该写成“hello.action"。

不过,前提是没有写<package></package>中的namespace属性,或namespace="/"。

如果namespace="/xxx/yyy"。则应该在表单中写成:

<form action="xxx/yyy/actionName.action" method="post|get"></form>

注意,namespace必须要以“/”开头,否则会出现找不到xxx Action。这有点类似在servlet配置中的url-pattern。

6.在struts2标签中使用表达式时。要特别注意:

A:要开头处要添加:

  <%@ taglib prefix="s" uri="/struts-tags" %>

B:其次在web.xml中要添加struts2的核心控制器:

  <filter>
      <filter-name>struts2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
      <url-pattern>/*</url-pattern>

  </filter-mapping>

C:在app/WEB-INF/lib中要添加相应的struts2的库。

要不会报错的。

<s:set name="age" value="61" />

<s:if test="%{age>60}" >

display result contents

</s:if>

7.国际化:

A:全局方面:

在app/WEB-INF/classes/struts.xml中配置:

<constant name="struts.custom.i18n.resources" value="globalMessages"></constant>

然后在app/WEB-INF/classes/文件夹下建立国际化资源文件,baseName_language_contry.properties 比如:

globalMessages_en_US.properties

globalMessages_zh_CN.properties

如果想要以特定的文件夹来存放国际化资源文件,则可这样子配置(假设你放在app/WEB-INF/classes/i18n/文件夹下),则要这样子配置:

在app/WEB-INF/classes/struts.xml中配置:

<constant name="struts.custom.i18n.resources" value="i18n/globalMessages"></constant>

app/WEB-INF/classes/i18n/文件夹下的文件为:

globalMessages_en_US.properties

globalMessages_zh_CN.properties

B:不用在配置文件里配置的方法:

在app/WEB-INF/classes/ 里放置你的国际化资源文件:xxxxx.properties

然后的jsp中调用时:

<i18n name="the/path/to/xxxxx" >

   <s:text name="your KEY" />

</i18n>

注意:<i18n>中name的属性必须是国际化资源文件的全名(除了".properties"后缀名,后缀名一定不要写进去!)。

C:动态改变国际化(前提当然是你要准备好这些国际化文件咯^_^):在请求Action时,添加一个参数:

xxxx.action?request_locale=zh_CN, 这个表示的是中文。

xxxx.action?request_locale=en_US, 这个表示的是英文。

8.标签:

A:set

@

<s:set name="username" value="yangzhiyong">

</s:set>

<s:if test='{#username=="yangzhiyong"}'>

   welcome , yang zhi yong!

</s:if>

<s:else>

  sorry, you are not yang zhi yong!

</s:else>

@#{"key":"value"}, 注意单引号与双引号的位置。

<s:set name="username" value='#{"first":"yang", "last":"zhiyong"}'>

</s:set>

<s:if test='{#username["first"]=="yang"}'>

   welcome , yang !

</s:if>

<s:else>

  sorry, you are not yang!

</s:else>

@与数字比较大小(注意,不用大括号的哦):

<s:set name="year" value="61">

</s:set>

<s:if test="#name > 61">(或者写成: <s:if test="#name gt 61"> 也可以)

          more than 61

</s:if>

<s:elseif test="#name < 61"> (或者写成 <s:elseif test="#name lt 61"> 也可以)

         less than 61

</s:elseif>

<s:else>

       equal 61

</s:else>


9.Struts2跳转时CSS失效问题:

解决方法 :

<% 
String contextPath=request.getContextPath(); //这里获取的是当前运行的webApp名称

String basePath = request.getScheme()+"://" + request.getServerName()+":"+ request.getServerPort()+contextPath;

       basePth的结果是http://addrress:port/webApp

      例如:http://localhost:8080/mywebApp

     注意是最后是没有"/"的哦....

%>


 10.配置拦截器的问题

 如果在struts.xml文件中的Action,没有添加使用拦截器,则默认的拦截器将会起作用.但是,当一旦为该包中的Action添加使用了某个拦截器,那么这个默认的拦截器就不会起作用了.这时,就要求手动地添加该默认的拦截器.

<intercepter-ref name="defaultStack"></interceptor-ref>

11. 在struts.xml中配置action时,如果有相同的名字的action(且在同一个包),则只会有最后一个action生效

12. 分割路径的问题:在unix和linux都适用.

  1. 由于unix中file.separator为斜杠"/",下面这段代码可以处理windows和unix下的所有情况:  
  2.   
  3. String temp[] = name.replaceAll("\\\\","/").split("/"); 


13. 如果Action中的字段与Model中的字段相同, 则Model的优先.


14.Struts2标签是可以嵌套使用的:

<s:iterator value="#attr.listAllOfUserLoginInfos" id="userLoginInfo" status="obj_status">

<s:property value="#attr.listPageOfUserBlogInfos.get(#obj_status.count-1).blogName" />

</s:iterator>


15.重定向Action:

<action name="login" class="org.android.action.user.UserAction" method="login">
           <result type="redirectAction" name="success">
           		<param name="actionName">listAllFilesAction</param>
           </result>
           <result name="input">/jsp/login.jsp</result>
        </action>
        
        <action name="listAllFilesAction" class="org.android.action.file.ListAllFilesAction" method="list">
        	<result name="success">/jsp/listOwnFiles.jsp</result>
        </action>

16.关于在<s:property value="xx" />中显示HTML格式的内容时。请使用escape属性来决定是否解析HTML格式的内容。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值