struts2学习笔记(持续更新)
-
struts.xml配置:要打开开发模式,即把value的值设为true,否则改动需要重启
-
namespace决定action的访问路径,默认为"",可以接收所有路径的action。namespace可以写为"/","/xxx","/xxx/yyy"对应的action路径为/index.action,/xxx/index.action,/xxx/yyy/index.action
-namespace决定action的访问路径,默认为"",可以接收所有路径的action。namespace可以写为"/","/xxx","/xxx/yyy"对应的action路径为/index.action,/xxx/index.action,/xxx/yyy/index.action -
路径问题,若action 跳转到某个jsp页面,这个jsp页面又有跳转链接,不能设置相对路径,要设置绝对路径,用basePath来设置,
<%String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
,①<a href=basePath+"index.jsp"></a>
②<base href="<%=basePath%>"> <a href="index.jsp></a>
-
开发模式不能动态修改result中的内容,若修改result中的需要重启服务器。
` -
struts2.5默认是严格调用,要想使用DMI,struts.xml要做一下设置
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
strict-method-invocation="false"struts2.5默认是true,要设置为false -
Action总结
-
chain跳转问题:首先,我先来查看一下,下载struts2里面的docs文件,从里面的index文件中可以查看相关的内容
Parameters*
actionName (default) - the name of the action that will be chained to.
namespace - used to determine which namespace the Action is in that we’re chaining. If namespace is null, this defaults to the current namespace.
method - used to specify another method on target action to be invoked. If null, this defaults to execute method.
skipActions - (optional) the list of comma separated action names for the actions that could be chained to.
*
Examples*
<package name="public" extends="struts-default">
<!-- Chain creatAccount to login, using the default parameter -->
<action name="createAccount" class="...">
<result type="chain">login</result>
</action>
<action name="login" class="...">
<!-- Chain to another namespace -->
<result type="chain">
<param name="actionName">dashboard</param>
<param name="namespace">/secure</param>
</result>
</action>
</package>
<package name="secure" extends="struts-default" namespace="/secure">
<action name="dashboard" class="...">
<result>dashboard.jsp</result>
</action>
</package>
从给出的example可以看出,总共有三种跳转方法,第一种是跳到同一个package中,第二个是跳到不同package中,这里着重讲解第二种方法,在result中加两个param参数,name里面的内容是写死的,不要去修改它,第一个参数对应着你要跳到的action的名称,第二个是你要跳到的package里的namespace
- Result总结
- 用Domain Model的时候,pojo要么不写构造方法(编译器会自动帮你生成无参的构造方法),如果写了构造方法,就要把无参的默认方法写上去,要不然会报错
- 通配符问题,当我设置为"_“的时候,可能会有匹配错误,即不论是不是应该匹配到这个通配符的action,都匹配进去了,造成错误,目前的解决方法,只能把中间的"_"设复杂点,测试”__",即两个_,又可以正常运行了
- ajax中的dataType是回传数据类型
- 使用struts2.5一定要记得把strict-method-invocation设为false
- jquery的对标签做操作,定义函数的时候,需取class来操作,即 ( " . c l a s s " ) , 不 可 以 用 i d 操 作 , 即 (".class"),不可以用id操作,即 (".class"),不可以用id操作,即("#id")