struts2学习 - action - 1

从以上路径copy struts.xml文件。加入到自己的工程的src文件夹下面。 这样经过编译,会在WEB-INF/classes文件夹中生成struts.xml

从以上路径copy struts2需要的类库。放入自己工程的lib文件夹中。

打开web.xml  copy出对struts的配置。

 

<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>


注意点:

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

ng:下一代。 用的是2.1以后的struts2. 2.0是没有ng的。


示例的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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" /><!--true:开发者模式 开发模式下,修改struts的配置文件后不需要重启tomcat服务器即生效-->
<!--关键字extends的使用:我想在下面的这个配置包中使用上面那个包中的结果集,那么可以使用extends关键字继承上一个包
<package name="default" namespace="/" extends="struts-default"><!-- namespace 对应项目名称后的‘/’
-->
<default-action-ref name="index" /><!-- 默认的action 配置文件中没有用户要访问的Aciton的时候,会调用<default-action-ref 所定义的Action --处理错误页面-->

<!-- 配置全局的结果集:当返回的result没有配置的时候会执行全局结果集的配置-->
<global-results>
<result name="error">/error.jsp</result>
</global-results>

<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>

<action name="index">
<result type="redirectAction"><!-- result有四个type 四种配置分别代表了四种不同的跳转方式 -->
<!--type不写默认的为dispatcher:用服务器跳转跳转到结果页面--只能跳转到页面,不能跳转到Action -->
<!--客户端跳转的方式跳转到结果页面 只能跳转到页面,不能跳转到Action type="redirect"-->
<!--客户端跳转的方式跳转到Action type="redirectAction-->
<!--服务器跳转的方式跳转到Action type="chain" -->
<!--当要跳转的Action在别的包下的时候 应该这样写 写明actionName 和namespace-->
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
</package>

<include file="example.xml"/>

<!-- Add packages here -->

</struts>



关于namespace:

namespace决定了action的访问路径,默认为"",可以接收所有路径的action
namespace可以写为/,或者/xxx,或者/xxx/yyy,对应的action访问路径为/index.action,
/xxx/index.action,或者/xxx/yyy/index.action
namespace最好也用模块来进行命名



struts1 和struts2的重要区别:

在struts1中进行多次访问的时候用到的是同一个Action对象。而struts2则是每一次访
问都会创建一个Action对象,所以struts2不会有线程安全问题的发生



 及其简单的action:

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

 

<result>标签如果没有指定name属性默认返回值为success

 


路径问题:

 

struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。虽然可以用redirect方式解决,但redirect方式并非必要。
解决办法非常简单,统一使用绝对路径(在jsp中用request.getContextRoot方式来拿到webapp的路径)
或者
使用myeclipse经常用的,指定basePath

<%
String
path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<base href="<%=basePath%>" />

<a href="index.jsp">index.jsp</a>

所以再写连接的时候就可以按照namespace的方式写了。
request.getContextPath()得到项目的名字,一般用来解决路径问题 如果项目为根目录,则得到一个"",即空的字条串。
request.getscheme()返回的协议名称.默认是http
request.getServerName()就是获取你的网站的域名,如果是在本地的话就是localhost
request.getServerPort()获取服务的端口号

 


PS:struts.xml不自动提示的解决办法: window--perferences-XML catalog---add---key
Type="URI" key="*.dtd"


 

 

 

posted @ 2012-04-08 09:22  大陶陶  阅读(448)  评论(0编辑  收藏  举报