struts集成restful

来源:互联网 发布:淘宝发布图片规则 编辑:程序博客网 时间:2024/06/07 01:21
参考网址:
http://blog.csdn.net/wangli61289/article/details/36662713

1、引入需要的JAR包
  <!-- struts2依赖包 -->
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-core</artifactId>
   <version>2.3.14</version>
  </dependency>

  <!-- struts restful 依赖包 -->
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-convention-plugin</artifactId>
   <version>2.3.14</version>
  </dependency>
  <dependency>
   <groupId>org.apache.struts</groupId>
   <artifactId>struts2-rest-plugin</artifactId>
   <version>2.3.14</version>
  </dependency>

2、修改struts.xml配置文件
<struts>
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.devMode" value="false" />
    <!-- struts2 restful -->
    <constant name="struts.convention.action.suffix" value="Controller"/>
    <constant name="struts.convention.action.mapAllMatches" value="true"/>
    <constant name="struts.convention.default.parent.package" value="rest-default"/>
   
    <include file="struts-default.xml" />
</struts>

3、修改web.xml配置文件
<web-app>
  <display-name>Archetype Created Web Application</display-name>
 <init-param>
  <param-name>config</param-name>
  <!-- <param-value>../../resources/struts.xml</param-value> -->
  <param-value>classpath:struts.xml</param-value>
 </init-param>
 
 <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>
    <!-- log4j -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>WEB-INF/log4j.properties</param-value>
    </context-param>
 <welcome-file-list>
  <welcome-file>login.jsp</welcome-file>
 </welcome-file-list>
</web-app>

4、编写Controller
Controller上面使用如下注解
@Results({
    @Result(name="success", type="redirectAction", params = {"actionName" , "orders"})
})

5、编写JSP页面
JSP页面需要放到WEB-INF下面的content目录下,页面命名采用controller中的actionName+方法名;
如orders-index.jsp、orders-show.jsp

参考代码:
http://download.csdn.net/detail/hutudanvip/8394941
0 0
原创粉丝点击