003_Hello Struts(Struts2环境搭建)

来源:互联网 发布:曼彻斯特大学 知乎 编辑:程序博客网 时间:2024/05/01 09:54

搭建 Struts2 的环境

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

jar包

  • 在 web.xml 文件中配置 struts2: 复制 struts\apps\struts2-blank1\WEB-INF\web.xml 文件中的过滤器的配置到当前 web 应用的 web.xml 文件中

  • 在当前 web 应用的 classpath 下添加 struts2 的配置文件 struts.xml: 复制 struts1\apps\struts2-blank\WEB-INF\classes 下的 struts.xml 文件到当前 web 应用的 src 目录下.

添加 DTD 约束

微信公众号:JavaWeb架构师

微信公众号:JavaWeb架构师

微信公众号:JavaWeb架构师

微信公众号:JavaWeb架构师

struts-2.3.4-all\struts-2.3.4\src\core\src\main\resources

微信公众号:JavaWeb架构师

步骤

  • 编辑web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    id="WebApp_ID" version="2.5">    <!--          优点:            1.不需要手动的定义Filter,通过Struts2的配置文件完成即可。            2.El表达式更简单:${requestScop.product.produceName} ===> ${productName}    -->    <!-- 配置 Struts2 的 Filter -->    <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>
  • 编辑struts.xml
<?xml version="1.0" encoding="UTF-8" ?>  <!DOCTYPE struts PUBLIC      "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"      "http://struts.apache.org/dtds/struts-2.3.dtd">  <struts>      <!--          注意:            1.关于lib包:太多了可能会存在冲突,反正就按照现在的lib包使用,缺少的时候再添加。    -->    <!--         1.配置包(用来组织模块)            1.1.package:新建包,组织模块。            1.2.name必须属性:用户指定包名(模块名),其他包继承的就是这个name的值。            1.3.extends:继承的包(就是上面的name值),会继承所有的配置,                一般继承struts-default(在struts-default.xml文件中,                struts.xml文件在:struts2-core.jar下面),文件已经放在根目录下面了。            1.4.namespace可选属性:命名空间,默认是/,写了命名空间之后,                访问就是(在项目文件夹下面多了一层命名空间,这样才能访问到):                    http://localhost:8080/Struts2_Course_002_Hello/命名空间/product-save.action                这个程序要:                    http://localhost:8080/Struts2_Course_002_Hello/test/product-input.action 这样才能正常跳转                如果不写或者写/,那直接访问就行:                    http://localhost:8080/Struts2_Course_002_Hello/product-input.action     -->    <package name="default" namespace="/test" extends="struts-default">              <!-- product-input.action -->        <action name="product-input">              <!-- 定义逻辑视图和物理资源之间的映射 -->              <result>/WEB-INF/pages/input.jsp</result>          </action>              <!--                  2.指定action:一个struts2的请求就是一个action                    2.1.name:对应的struts2请求名,这里就是:product-input.action(对应servletPath,但是去掉/.*)                2.2.class属性:需要反射的类的全类名                    有一个默认值,不写的时候:                        com.opensymphony.xwork2.ActionSupport                    2.3.method属性:调用反射类的方法【这个方法的返回值很特殊,要注意】                        有一个默认的方法execute:                            public String execute() throws Exception {                                return SUCCESS;                            }            -->        <action name="product-save" class="top.itcourse.hello.Product" method="save">                <!--                      3.指定struts2请求的转发结果                        3.1.name属性:指定情况(上面的method方法返回的字符串,会和name属性匹配,匹配到的就执行)                            有一个默认值:success                        3.2.标签体:资源的路径                        3.3.type属性,可选:表示结果的类型,默认值是dispatcher(转发),redirect是重定向                -->                <result name="details">/WEB-INF/pages/details.jsp</result>        </action>    </package>  </struts>  
  • 编辑index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>    <!--          过程:            1.用户点击a标签            2.请求发送到相应的action(product-input.action)            3.请求将会被web.xml中配置的struts2所拦截            4.每个action就是一个struts2请求,将会去到struts.xml文件中接受逻辑的调配    -->    <!-- test/是命名空间 -->    <a href="test/product-input.action">输入</a>    <br><br>    <a href="test.action">测试</a></body></html>
  • 编辑Product.java
    // 将会被struts.xml中的action所反射调用该方法        public String save(){        System.out.println("save: " + this);        // 用于对应action下面的result的name属性        return "details";    }


其它



- 源码下载:

关注下方公众号回复:Hello Struts(Struts2环境搭建).code
  • 欢迎加入交流群:451826376

  • 更多信息:www.itcourse.top

完整教程PDF版本下载

原创粉丝点击