struts2框架怎么运用

发布时间:2022-09-29 15:08:15 作者:iii
来源:亿速云 阅读:193

Struts2框架怎么运用

1. 引言

Struts2是一个基于MVC设计模式的Web应用框架,它是Struts1的升级版本,提供了更加灵活和强大的功能。Struts2框架的核心思想是将请求处理、业务逻辑和视图展示分离,使得开发者能够更加专注于业务逻辑的实现,而不必过多关注底层的技术细节。

本文将详细介绍Struts2框架的基本概念、核心组件、配置方法以及如何在实际项目中运用Struts2框架进行开发。

2. Struts2框架概述

2.1 MVC设计模式

MVC(Model-View-Controller)是一种软件设计模式,它将应用程序分为三个核心部分:

Struts2框架正是基于MVC设计模式,通过将请求处理、业务逻辑和视图展示分离,使得开发者能够更加高效地开发Web应用。

2.2 Struts2框架的核心组件

Struts2框架的核心组件包括:

3. Struts2框架的配置

3.1 配置web.xml

在使用Struts2框架之前,首先需要在web.xml文件中配置Struts2的核心过滤器。以下是一个典型的web.xml配置示例:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

在这个配置中,StrutsPrepareAndExecuteFilter是Struts2框架的核心过滤器,它负责处理所有的请求,并将请求分发到相应的Action进行处理。

3.2 配置struts.xml

struts.xml是Struts2框架的核心配置文件,用于定义Action、拦截器、结果等。以下是一个简单的struts.xml配置示例:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="hello" class="com.example.HelloAction">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

在这个配置中,我们定义了一个名为hello的Action,它对应的Java类是com.example.HelloAction。当请求hello.action时,Struts2框架会调用HelloAction类中的execute方法,并根据返回的结果(success)跳转到hello.jsp页面。

3.3 配置struts.properties

struts.properties文件用于配置Struts2框架的一些全局属性。以下是一个简单的struts.properties配置示例:

struts.devMode = true
struts.i18n.encoding = UTF-8

在这个配置中,struts.devMode设置为true,表示启用开发模式,这样在开发过程中可以更方便地调试和查看错误信息。struts.i18n.encoding设置为UTF-8,表示使用UTF-8编码来处理请求和响应。

4. Struts2框架的核心组件详解

4.1 Action

Action是Struts2框架中处理用户请求的核心组件。每个Action通常对应一个业务逻辑,开发者可以通过继承ActionSupport类或实现Action接口来创建自定义的Action类。

以下是一个简单的Action类示例:

package com.example;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String execute() throws Exception {
        message = "Hello, Struts2!";
        return SUCCESS;
    }
}

在这个示例中,HelloAction类继承自ActionSupport,并重写了execute方法。当请求hello.action时,execute方法会被调用,并返回SUCCESS,表示处理成功。

4.2 Interceptor

拦截器是Struts2框架中的一个重要概念,它可以在Action执行前后进行一些预处理或后处理操作。Struts2框架提供了许多内置的拦截器,开发者也可以自定义拦截器。

以下是一个简单的自定义拦截器示例:

package com.example;

import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.ActionInvocation;

public class MyInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("Before Action Execution");
        String result = invocation.invoke();
        System.out.println("After Action Execution");
        return result;
    }
}

在这个示例中,MyInterceptor类继承自AbstractInterceptor,并重写了intercept方法。在intercept方法中,我们可以在Action执行前后输出一些日志信息。

要在struts.xml中配置这个拦截器,可以使用以下配置:

<struts>
    <package name="default" extends="struts-default">
        <interceptors>
            <interceptor name="myInterceptor" class="com.example.MyInterceptor"/>
        </interceptors>

        <action name="hello" class="com.example.HelloAction">
            <interceptor-ref name="myInterceptor"/>
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

在这个配置中,我们定义了一个名为myInterceptor的拦截器,并将其应用到hello Action中。

4.3 Result

Result是Struts2框架中用于处理Action执行结果的组件。Struts2框架支持多种类型的Result,包括JSP、FreeMarker、Velocity等。

以下是一个简单的Result配置示例:

<struts>
    <package name="default" extends="struts-default">
        <action name="hello" class="com.example.HelloAction">
            <result name="success">/hello.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

在这个配置中,我们定义了两个Result,分别对应successerror。当Action返回success时,Struts2框架会跳转到hello.jsp页面;当Action返回error时,Struts2框架会跳转到error.jsp页面。

4.4 Value Stack

值栈是Struts2框架中用于在Action和视图之间传递数据的机制。值栈是一个栈结构,开发者可以通过OGNL表达式访问值栈中的数据。

以下是一个简单的值栈使用示例:

package com.example;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport {
    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    @Override
    public String execute() throws Exception {
        message = "Hello, Struts2!";
        return SUCCESS;
    }
}

在这个示例中,message属性被存储在值栈中。在JSP页面中,我们可以通过OGNL表达式访问这个属性:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello Struts2</title>
</head>
<body>
    <h1><s:property value="message"/></h1>
</body>
</html>

在这个JSP页面中,<s:property value="message"/>标签会从值栈中获取message属性的值并显示在页面上。

4.5 OGNL

OGNL(Object-Graph Navigation Language)是Struts2框架中用于访问和操作对象的表达式语言。OGNL可以用于访问值栈中的对象、调用对象的方法、访问集合等。

以下是一个简单的OGNL使用示例:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Hello Struts2</title>
</head>
<body>
    <h1><s:property value="message"/></h1>
    <h2><s:property value="user.name"/></h2>
</body>
</html>

在这个JSP页面中,<s:property value="user.name"/>标签会从值栈中获取user对象的name属性并显示在页面上。

5. Struts2框架的实际应用

5.1 创建Struts2项目

在实际项目中,我们可以使用Maven或Gradle等构建工具来创建Struts2项目。以下是一个使用Maven创建Struts2项目的示例:

<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.example</groupId>
    <artifactId>struts2-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.26</version>
        </dependency>
    </dependencies>
</project>

在这个pom.xml文件中,我们添加了struts2-core依赖,这是Struts2框架的核心依赖。

5.2 编写Action类

在项目中,我们可以编写多个Action类来处理不同的业务逻辑。以下是一个简单的Action类示例:

package com.example;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String execute() throws Exception {
        if ("admin".equals(username) && "123456".equals(password)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
}

在这个示例中,LoginAction类用于处理用户登录请求。如果用户名和密码正确,则返回SUCCESS,否则返回ERROR

5.3 配置struts.xml

struts.xml文件中,我们可以配置多个Action来处理不同的请求。以下是一个简单的struts.xml配置示例:

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <package name="default" extends="struts-default">
        <action name="login" class="com.example.LoginAction">
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
</struts>

在这个配置中,我们定义了一个名为login的Action,它对应的Java类是com.example.LoginAction。当请求login.action时,Struts2框架会调用LoginAction类中的execute方法,并根据返回的结果跳转到相应的页面。

5.4 编写JSP页面

在项目中,我们可以编写多个JSP页面来展示不同的视图。以下是一个简单的登录页面示例:

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <s:form action="login">
        <s:textfield name="username" label="Username"/>
        <s:password name="password" label="Password"/>
        <s:submit value="Login"/>
    </s:form>
</body>
</html>

在这个JSP页面中,我们使用了Struts2的标签库来创建表单。当用户提交表单时,请求会被发送到login.action,并由LoginAction类进行处理。

5.5 运行项目

在完成以上步骤后,我们可以将项目部署到Tomcat等Servlet容器中,并访问相应的URL来测试Struts2框架的功能。

6. 总结

Struts2框架是一个功能强大且灵活的Web应用框架,它基于MVC设计模式,通过将请求处理、业务逻辑和视图展示分离,使得开发者能够更加高效地开发Web应用。本文详细介绍了Struts2框架的基本概念、核心组件、配置方法以及如何在实际项目中运用Struts2框架进行开发。希望通过本文的介绍,读者能够掌握Struts2框架的基本使用方法,并能够在实际项目中灵活运用。

推荐阅读:
  1. 模块及运用
  2. arp协议及运用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

struts2

上一篇:win10电脑中怎么打开运行命令

下一篇:win10笔记本插电源就会蓝屏如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》