zoukankan      html  css  js  c++  java
  • Struts2(一)创建一个Struts2工程

    一.环境准备

      idea+maven

    二.引入依赖

    Struts2所需要的jar包

    struts2-core-2.0.6.jar Status2的核心类库

    xwork-2.0.6.jar Command模式框架,webwork和strues2都基于xwork

    ognl-2.6.11.jar 对象图导航语言,struts2通过其读写对象属性

    freemarker-2.3.8.jar status2的UI标签模板通过Freemarker编写

    commons-logging-1.0.4.jar struts2通过这个包来支持log4j和jdk1.4+的日志记录

    commons-fileupload-1.2.2.jar 文件上传组件struts2 2.1.6后新增的

    commons-io-2.0.1.jar 传文件所依赖的jar包

    commons-lang-2.5.jar 对java.lang的增强

    javassist-3.11.0.GA.jar 代码生成工具 struts2用它在运行时扩充java类

    所需依赖

     <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.5.5</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.opensymphony/xwork-core -->
            <dependency>
                <groupId>com.opensymphony</groupId>
                <artifactId>xwork-core</artifactId>
                <version>2.1.6</version>
            </dependency>
            <!-- java ee的jar-->
            <dependency>
                <groupId>javax</groupId>
                <artifactId>javaee-api</artifactId>
                <version>6.0</version>
                <scope>provided</scope>
            </dependency>

    三.编写web.xml文件

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <!-- 拦截所有的action -->
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

    四.编写Action

    在struts2中一个普通类想要变成Action需要实现Action接口

    import com.opensymphony.xwork2.Action;
    
    public class HelloWorldAction implements Action{
        private String name ;
        private String message;
        public String execute() throws Exception {
            setMessage("Hello"+getName());
            return "success";
        }
    }

    五.src下创建名称为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>
        <!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat -->
        <constant name="struts.devMode" value="true" />
    
        <package name="default" namespace="/" extends="struts-default">
            <!-- 第一个action的例子 -->
            <action name="helloWorld" class="cn.happy.action.HelloWorldAction">
                <result name="success">
                   index.jsp
                </result>
            </action>
            <!-- 登陆的action -->
        </package>
        <!-- Add packages here -->
    
    </struts>

     

    六.编写index.jsp页面

    <div>
            <h1>
                <!--显示Struts Action中message的属性内容-->
                <s:property value="message"/>
            </h1>
        </div>
        <div>
            <form action="helloWorld.action" method="post"> 
                请输入您的姓名: 
                <input name="name" type="text" />
                <input type="submit" value="提交" />
            </form>
        </div>

    然后启动tomcat运行即可。

  • 相关阅读:
    Springboot整合activeMq
    linux下安装activeMq
    linux下配置jdk
    thinkphp5 集成 redis
    linux下安装redis
    tp5集成swagger
    lombok
    idea下springboot环境搭建
    Mac环境下maven安装配置
    idea环境下搭建swagger2接口文档
  • 原文地址:https://www.cnblogs.com/wy0119/p/8483848.html
Copyright © 2011-2022 走看看