欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

搭建 structs2 环境

程序员文章站 2022-06-13 09:43:07
前言 环境: window 10 ,JDK 1.8 ,Tomcat 7 ,MyEclipse 2014 pro 搭建 SSH 环境的步骤 1. 创建 JavaWeb 项目 2. 导入 structs2 的jar包,只需导入13个基础 jar 包即可,后续 jar 包根据需要导入。 asm 3.3.j ......

前言

环境: window 10 ,jdk 1.8 ,tomcat 7 ,myeclipse 2014 pro

搭建 ssh 环境的步骤

  1. 创建 javaweb 项目

  2. 导入 structs2 的jar包,只需导入13个基础 jar 包即可,后续 jar 包根据需要导入。

    asm-3.3.jar
    asm-commons-3.3.jar
    asm-tree-3.3.jar
    commons-fileupload-1.3.1.jar
    commons-io-2.2.jar
    commons-lang3-3.2.jar
    freemarker-2.3.22.jar
    javassist-3.11.0.ga.jar
    log4j-api-2.2.jar
    log4j-core-2.2.jar
    ognl-3.0.6.jar
    struts2-core-2.3.24.jar
    xwork-core-2.3.24.jar

  3. web.xml 配置 structs2 核心过滤器(很重要)。

    <?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_3_0.xsd" 
             version="3.0">
      <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>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
  4. 定义处理用户请求的 action 类

package cn.itcast.action;
import com.opensymphony.xwork2.actionsupport;
public class helloworldaction extends actionsupport{    
    public string execute() throws exception {
        return success;
    }
}

在项目目录 src 下,新建一个 structs.xml,书写下面的代码。

<?xml version="1.0" encoding="utf-8"?>
<!-- 指定struts2配置文件的dtd信息 -->
<!doctype struts public
    "-//apache software foundation//dtd struts configuration 2.3//en"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<!-- struts2配置文件的根元素 -->
<struts>
  <!-- struts2的action必须放在指定的包空间下定义 -->
  <package name="hello" extends="struts-default" namespace="/*">
    <!-- 定义 action,该action对应的类为cn.itcast.action.helloworldaction类 
    -->
    <action name="helloworld" class="cn.itcast.action.helloworldaction">
      <!-- 定义处理结果和视图资源之间的映射关系 -->
      <result name="success">/success.jsp</result>
    </action>
  </package>
</struts>
  1. 创建访问视图

    1. index.jsp
    <%@ page language="java" contenttype="text/html; charset=utf-8"
       pageencoding="utf-8"%>
    <html>
     <head>
         <title>首页</title>
    </head>
     <body>
         <h1>welcome to struts 2!</h1>
         <a href="${pagecontext.request.contextpath }/helloworld.action">
             hello world
         </a>
     </body>
    </html>
    
    1. success.jsp
    <%@ page language="java" import="java.util.*" pageencoding="utf-8"%>
    <!doctype html public "-//w3c//dtd html 4.01 transitional//en">
    <html>
     <head>
         <title>成功页面</title>
     </head>
     <body>
         欢迎学习第一个struts2程序!
     </body>
    </html>
    
  2. 效果图

搭建 structs2 环境

总结

注意别忘记在 web.xml 里配置 structs2 核心过滤器。

helloaction 类继承 actionsupport 要覆写父类的 execute 方法。

execute 方法中返回值 success 是常量。