Apache Struts 2入门指南

简介: 版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/48877399 Apache Struts 2入门指南作者:chszs,版权所有,未经同意,不得转载。
版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/48877399

Apache Struts 2入门指南

作者:chszs,版权所有,未经同意,不得转载。博主主页:http://blog.csdn.net/chszs

本文使用最新的Struts 2.3.24.1版,演示了怎样用Apache Struts 2构建最基本的Web应用。

项目的基本需求:

1)Maven 3.3.3
2)Eclipse Mars.1 Release (4.5.1)
3)Struts 2.3.24.1

一、项目的主体结构

1、新建一Maven项目

Group Id:com.ch.common
Artifact Id:Struts2Example
Packaging:war

2、解决Maven Web项目的一个错误

这里写图片描述

鼠标右键点击项目,选择”Java EE Tools”->”enerate Deployment Descriptor Stub”,会自动产生WEB-INF子目录和web.xml配置文件。

3、导入Struts 2依赖包

项目pom.xml内容如下:

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.ch.common</groupId>
    <artifactId>Struts2Example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.24.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>Struts2Example</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

4、项目的主体结构如下图所示

这里写图片描述

二、JSP页面

1、编写JSP登录页面

包括输入用户名和密码的输入框、提交按钮等。
login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
    <h1>Struts 2 Hello World Example</h1>
    <s:form action="Welcome">
        <s:textfield name="username" label="Username" />
        <s:password name="password" label="Password" />
        <s:submit />
    </s:form>
</body>
</html>

2、编写JSP欢迎页面

登录成功后,进入欢迎页面。
welcome_user.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head></head>
<body>
    <h1>Struts 2 Hello World Example</h1>
    <h2>
        Hello
        <s:property value="username" />
    </h2>
</body>
</html>

3、编写业务逻辑Action类

Struts 2的Action类,负责所有的业务逻辑。
WelcomeUserAction.java

package com.ch.user.action;
public class WelcomeUserAction {
    private String username;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    // Struts业务逻辑放这里
    public String execute(){
        return "SUCCESS";
    }
}

在Struts 2中,Action类无需实现任何借口或继承任何类,唯一的要求就是它必须创建一个execute()方法来放置所有的业务逻辑,并且此方法必须返回String类型的字符串,告知用户它要重定向到哪里。
注意:有些开发者实现了com.opensymphony.xwork2.Action类,这取决于你的需求和应用场景,这个类提供了常用的常量值。

4、Struts 2的配置文件

Struts 2的配置文件名必须是struts.xml。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="user" namespace="/pages" extends="struts-default">
        <action name="Login">
            <result>login.jsp</result>
        </action>
        <action name="Welcome" class="com.ch.user.action.WelcomeUserAction">
            <result name="SUCCESS">welcome_user.jsp</result>
        </action>
    </package>
</struts>

Struts配置文件声明了一个包(Package)和封装的Action类,Action类是自解释的,下面对配置中的一些内容做一说明:

1)package name=”user”
仅仅定义了一个包名,无需关心它。

2)namespace=”/pages”
这用于匹配URL为“/”的访问路径。

3)extends=”struts-default”
意思是此包继承自struts-default包组件和拦截器,而这些是在struts-default.xml文件中声明的,这个配置文件位于struts2-core.jar文件中。

5、web.xml配置

Web应用描述符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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <display-name>Struts 2 Web Application</display-name>
    <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>pages/login.jsp</welcome-file>
    </welcome-file-list>
</web-app>

6、运行项目

对于Struts 2 Web项目,可以通“.action”后缀直接访问Action。
浏览器访问:http://localhost:8080/Struts2Example/pages/Login.action
或者是访问:http://localhost:8080/Struts2Example/pages/login.jsp
这里写图片描述
任意输入用户名和密码,
这里写图片描述
可以看到,访问正常!

目录
相关文章
|
5月前
|
SQL 算法 Java
Apache Calcite入门
Apache Calcite入门
160 0
|
5月前
|
分布式计算 Java 大数据
Apache SeaTunnel 3 分钟入门指南
Apache SeaTunnel 3 分钟入门指南
238 0
|
5月前
|
数据采集 分布式计算 Hadoop
开源数据质量解决方案——Apache Griffin入门宝典(上)
开源数据质量解决方案——Apache Griffin入门宝典
165 0
|
6月前
|
SQL 消息中间件 分布式计算
Apache Doris 系列: 入门篇-数据导入及查询
Apache Doris 系列: 入门篇-数据导入及查询
157 0
|
6月前
|
SQL 关系型数据库 Apache
Apache Doris 系列: 入门篇-创建数据表
Apache Doris 系列: 入门篇-创建数据表
191 0
|
3月前
|
分布式计算 Java 大数据
IO流【Java对象的序列化和反序列化、File类在IO中的作用、装饰器模式构建IO流体系、Apache commons-io工具包的使用】(四)-全面详解(学习总结---从入门到深化)
IO流【Java对象的序列化和反序列化、File类在IO中的作用、装饰器模式构建IO流体系、Apache commons-io工具包的使用】(四)-全面详解(学习总结---从入门到深化)
53 0
|
1月前
|
监控 测试技术 Linux
性能工具之 Apache Bench 入门使用
ab 全称为:apache bench,ab 为小型压力工具,对于在 Linux 中简单压测 HTTP 接口轻巧灵活。
23 1
|
1月前
|
SQL 分布式计算 HIVE
Apache Hudi入门指南(含代码示例)
Apache Hudi入门指南(含代码示例)
56 0
|
6月前
|
存储 固态存储 关系型数据库
Apache Doris 系列: 入门篇-安装部署
Apache Doris 系列: 入门篇-安装部署
368 0
|
3月前
|
SQL 关系型数据库 Apache
Apache Doris 整合 FLINK CDC 、Paimon 构建实时湖仓一体的联邦查询入门
Apache Doris 整合 FLINK CDC 、Paimon 构建实时湖仓一体的联邦查询入门
664 1

热门文章

最新文章

推荐镜像

更多