码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2 的 Hello World demo程序

时间:2015-06-18 13:19:02      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:

  • 搭建 Struts2 的环境:
    • 加入 jar 包: 复制 struts\apps\struts2-blank\WEB-INF\lib 下的所有 jar 包到当前 web 应用的 lib 目录下.

      技术分享

    • 在 web.xml 文件中配置 struts2: 复制 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的过滤器的配置到当前 web 应用的 web.xml 文件中。
    • 代码如下:
      <?xml version="1.0" encoding="UTF-8"?>
      
      <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
      
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      
      xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
      
      version="3.1">
      
      <!--使用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>
      
      </web-app>

       

    • 在当前 web 应用的 classpath 下添加 struts2 的配置文件 struts.xml: 复制 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml 文件到当前 web 应用的 src 目录下。
      <?xmlversion="1.0"encoding="UTF-8"?>
      
      <!DOCTYPEstrutsPUBLIC
      
      "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
      
      "http://struts.apache.org/dtds/struts-2.3.dtd">
      
      <struts>
      
      <!--
      
      package:包.struts2使用package来组织模块.
      
      name属性:必须.用于其它的包应用当前包.
      
      extends:当前包继承哪个包,继承的,即可以继承其中的所有的配置.通常情况下继承struts-default
      
      struts-default这个包在struts-default.xml文件中定义.
      
      namespace可选,如果它没有给出,则以/为默认值.
      
      若namespace有一个非默认值,则要想调用这个包里的Action,
      
      就必须把这个属性所定义的命名空间添加到有关的URI字符串里
      
      http://localhost:8080/contextPath/namespace/actionName.action
      
      -->
      
      <packagename="helloWorld"extends="struts-default">
      
      <!--
      
      配置一个action:一个struts2的请求就是一个action
      
      name:对应一个struts2的请求的名字(或对一个servletPath,但去除/和扩展名),不包含扩展名
      
      class的默认值为:com.opensymphony.xwork2.ActionSupport
      
      method的默认值为:execute
      
      result:结果.
      
      -->
      
      <action name="product-input"
      
      class="com.opensymphony.xwork2.ActionSupport"
      
      method="execute">
      
      <!--
      
      result:结果.表示action方法执行后可能返回的一个结果.所以一个action节点可能会有多个result子节点.
      
      多个result子节点使用name来区分
      
      name:标识一个result.和action方法的返回值对应.默认值为success
      
      type:表示结果的类型.默认值为dispatcher(转发到结果.)
      
      -->
      
      <result name="success"type="dispatcher">/WEB-INF/pages/input.jsp</result>
      
      </action>
      
         
      
      <action name="product-save"class="com.cjm.struts2.helloworld.Product"
      
      method="save">
      
      <result name="details">/WEB-INF/pages/details.jsp</result>
      
      </action>
      
         
      
      <action name="test" class="com.cjm.struts2.helloworld.Product" method="test">
      
      <result>/index.jsp</result>
      
      </action>
      
         
      
      </package>
      
      </struts>

       

         

    • 技术分享

Struts2 的 Hello World demo程序

标签:

原文地址:http://www.cnblogs.com/auroracjm/p/4585315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!