初识struts2.5.12之 hello
原创    lido_me    发表于:2017-07-22 15:04:56
  阅读 :99   收藏   编辑

struts2其实就是为我们封装了servlet,简化了jsp跳转的复杂操作,并且提供了易于编写的标签,可以快速开发view层的代码。

过去,我们用jsp和servlet搭配,实现展现时,大体的过程是:

  1 jsp触发action

  2 servlet接受action,交给后台class处理

  3 后台class跳转到其他的jsp,实现数据展现

现在有了struts2,实现过程变为

  1 jsp出发action

  2 struts2拦截请求,调用后台action

  3 action返回结果,由不同的jsp展现数据

官网地址:http://struts.apache.org/

1.项目结构:

2.Maven pom 文件:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xx</groupId>
    <artifactId>struts</artifactId>
    <packaging>war</packaging>
    <version>1.0.0</version>
    <name>struts Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <jdk.version>1.7</jdk.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>

    </dependencies>
    <build>
        <finalName>struts</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                </configuration>
            </plugin>
            <!-- tomcat7:run -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/test</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <server>tomcat7</server>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
3.web xml 文件:
<!-- Filters -->
    <filter>
        <filter-name>action2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>action2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
4.struts xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <!-- DIM 动态配置 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 开发模式 -->
    <constant name="struts.devMode" value="true"></constant>

    <package name="default" extends="struts-default" namespace="/">
        <action name="print" class="com.xx.action.HelloAction" method="hi">
            <result>/hello.jsp</result>
        </action>
    </package>

</struts>
5.helloAction 文件:
package com.xx.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{


    public String hi(){

        return Action.SUCCESS;
    }

}
6.hello.jsp 文件:
<html>
<body>
<h2>Hello Struts!</h2>
</body>
</html>
7.运行:
tomcat7:run

浏览器访问:http://localhost:8080/test/print

评论
条评论

推荐阅读