原文: https://howtodoinjava.com/struts2/struts-2-hello-world-example-application/

在我以前的文章中,我写了许多关于 JAX-RS RESTEasySpring3Hibernate 和其他 Java 框架,例如 Mavenjunit 的示例和教程。 我也有很多要求在 Struts2 上写一些东西。 好吧,这里我从世界示例开始。 在下一篇文章中,我将尝试涵盖涉及 Struts2 的最大领域和概念。因此,请继续关注。

  1. Sections in this post:
  2. Create maven web project
  3. Struts2 dependencies
  4. web.xml changes
  5. Know struts.xml configuration file
  6. Using struts.properties file
  7. Writing first action class
  8. Composing view files
  9. Testing the application
  10. 源码下载

创建 Maven Web 项目

我在这里不会吃太多空间。 您可以阅读有关如何创建 Maven Eclipse Web 项目的更多详细信息。 简而言之,使用以下命令。

  1. mvn archetype:generate -DgroupId=com.howtodoinjava.struts2.example -DartifactId=struts2example -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  2. cd struts2example
  3. mvn eclipse:eclipse -Dwtpversion=2.0

Struts2 依赖项

我正在使用 maven 导入 Struts2 运行时依赖项。 这样做的好处是,您无需手动记住和寻找必需的依赖项,一举就能掌握所有内容。

  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-core</artifactId>
  4. <version>2.3.15.1</version>
  5. </dependency>

如果您想查看所有包含的内容,请查看下图:(获取最新版本的 jar 文件)

Struts2 HelloWorld 示例 - 图1

Struts2 jar 文件

修改后的web.xml

需要以某种方式将 Struts 插入您的 Web 应用。 这意味着应将对应用的传入请求移交给 strut 进行处理。 这是通过在web.xml文件中添加过滤器定义来完成的。 此筛选器实质上将所有传入请求重定向到StrutsPrepareAndExecuteFilter,然后使用配置来处理该请求。

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <filter>
  7. <filter-name>struts2</filter-name>
  8. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  9. </filter>
  10. <filter-mapping>
  11. <filter-name>struts2</filter-name>
  12. <url-pattern>/*</url-pattern>
  13. </filter-mapping>
  14. </web-app>

了解struts.xml配置文件

因此,StrutsPrepareAndExecuteFilter有一个要处理的请求。 怎么办? 它将使用该配置来知道如何处理特定请求。 此配置在 struts.xml 文件中定义。 该文件将具有特定于应用工作流及其操作处理器的 url 映射。 它还定义了输入/成功/错误视图。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4. "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <include file="struts-default.xml"/>
  7. <package name="default" extends="struts-default">
  8. <action name="">
  9. <result>/WEB-INF/jsp/index.jsp</result>
  10. </action>
  11. <action name="testAction" class="com.howtodoinjava.struts2.example.web.TestAction">
  12. <result name="input">/WEB-INF/jsp/index.jsp</result>
  13. <result name="success">/WEB-INF/jsp/success.jsp</result>
  14. <result name="error">/WEB-INF/jsp/index.jsp</result>
  15. </action>
  16. </package>
  17. </struts>

使用struts.properties文件

Struts 使用一些默认属性在运行时配置其行为。 您可以在stuts.properties文件中覆盖这些默认值。

  1. #see http://struts.apache.org/2.0.14/docs/strutsproperties.html
  2. struts.devMode=true
  3. //message resource
  4. struts.custom.i18n.resources=global

编写第一个动作类

这很重要,因为您将在此处编写实际的应用逻辑。 Struts2 动作通常扩展ActionSupport类,这些类提供了一些方法来覆盖和更改应用流,并在两者之间注入业务逻辑。

  1. package com.howtodoinjava.struts2.example.web;
  2. import java.util.Date;
  3. import com.opensymphony.xwork2.ActionSupport;
  4. @SuppressWarnings("serial")
  5. public class TestAction extends ActionSupport
  6. {
  7. private String name;
  8. private Date nowDate;
  9. @Override
  10. public void validate(){
  11. if (name==null || name.length()==0)
  12. addActionError(getText("error.enter.message"));
  13. }
  14. @Override
  15. public String execute() throws Exception {
  16. nowDate = new Date();
  17. return ActionSupport.SUCCESS;
  18. }
  19. public String getName() {
  20. return name;
  21. }
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25. public Date getNowDate() {
  26. return nowDate;
  27. }
  28. }

注意: Struts2 动作看起来像 POJO 类,因为它们必须充当动作形式,它们也是 Struts1 中的单独实体。

构建视图文件

这是一般步骤,涉及编写视图层,例如,在我们的例子中,我们正在编写 jsp 文件。 您可以使用消息资源从属性文件中获取消息,稍后在 i18n 中提供帮助。

index.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
  2. <%@ taglib prefix="s" uri="/struts-tags" %>
  3. <?xml version="1.0" encoding="UTF-8" ?>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html>
  6. <head>
  7. <title>Struts2 hello world example</title>
  8. <s:head/>
  9. </head>
  10. <body>
  11. <h1><s:text name="welcome" /></h1>
  12. <s:if test="hasActionErrors()">
  13. <div id="fieldErrors">
  14. <s:actionerror/>
  15. </div>
  16. </s:if>
  17. <s:form action="testAction" namespace="/" method="post" name="myForm" theme="xhtml">
  18. <s:textfield name="name" size="40" maxlength="40" key="your.message-label"/>
  19. <s:submit key="submit" />
  20. </s:form>
  21. </body>
  22. </html>

success.jsp

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2. pageEncoding="ISO-8859-1"%>
  3. <%@ taglib prefix="s" uri="/struts-tags" %>
  4. <?xml version="1.0" encoding="UTF-8" ?>
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  6. <html>
  7. <head>
  8. <title>Success Screen !!</title>
  9. </head>
  10. <body>
  11. <h2>Thank you for running this demo on <s:property value="nowDate" /></h2>
  12. <p>
  13. Your name recorded was: <h3><s:property value="name" /></h3>
  14. </p>
  15. </body>
  16. </html>

他们正在使用的消息资源文件是:

global.properties

  1. submit=Submit
  2. your.message-label=Your Name
  3. welcome=Welcome to Struts2!
  4. error.enter.message=Please enter your name !!

测试应用

现在好运行我们的 helloworld 应用。 让我们点击浏览器。

键入http://localhost:8080/struts2example/,然后按Enter

Struts2 HelloWorld 示例 - 图2

按提交而不输入任何内容

Struts2 HelloWorld 示例 - 图3

输入您的姓名,然后按提交

Struts2 HelloWorld 示例 - 图4

这就是 Struts2 helloworld 应用的全部朋友。 如果要下载本教程的源代码,请遵循以下给定的下载链接。

源码下载

学习愉快!