原文: https://howtodoinjava.com/struts2/struts-2-hello-world-with-annotations/

在我以前的文章中,我写了许多关于 JAX-RS RESTEasySpring3Hibernate 和其他 Java 框架,例如 Mavenjunit 的示例和教程。 我还写了与 Struts2 helloworld 和 xml 配置 相关的帖子。 在本文中,我将更新先前的示例,以使用注解来配置 Struts2 应用。

有关信息,struts 注解是 struts 常规插件的一部分。

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

创建 Maven Web 项目

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

  1. mvn archetype:generate -DgroupId=com.howtodoinjava.struts2.example -DartifactIad=struts2example
  2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  3. cd struts2example
  4. 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>
  6. <dependency>
  7. <groupId>org.apache.struts</groupId>
  8. <artifactId>struts2-convention-plugin</artifactId>
  9. <version>2.3.15.1</version>
  10. </dependency>
  11. </dependencies>

web.xml的更改

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

另外,我传递了“actionPackages”初始化参数,以便可以扫描此包以查找必需的带注解的类。

  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. <init-param>
  10. <param-name>actionPackages</param-name>
  11. <param-value>com.howtodoinjava.struts2.example.web</param-value>
  12. </init-param>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name>struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
  18. </web-app>

编写第一个类

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

  1. @Namespace("/default")
  2. @ResultPath(value="/")
  3. @Results({
  4. @Result(name="success", location="WEB-INF/jsp/success.jsp"),
  5. @Result(name="input", location="WEB-INF/jsp/index.jsp")
  6. })
  7. public class TestAction extends ActionSupport
  8. {
  9. private static final long serialVersionUID = 1L;
  10. private String name;
  11. private Date nowDate;
  12. @Override
  13. public void validate(){
  14. if (name==null || name.length()==0)
  15. addActionError(getText("error.enter.message"));
  16. }
  17. @Actions({
  18. @Action("/"),
  19. @Action("/test")
  20. })
  21. @Override
  22. public String execute() throws Exception {
  23. nowDate = new Date();
  24. return ActionSupport.SUCCESS;
  25. }
  26. public String getName() {
  27. return name;
  28. }
  29. public void setName(String name) {
  30. this.name = name;
  31. }
  32. public Date getNowDate() {
  33. return nowDate;
  34. }
  35. }

注意: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="test" 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>

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

TestAction.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 注解示例 - 图1

按提交而不输入任何内容

Struts2 HelloWorld 注解示例 - 图2

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

Struts2 HelloWorld 注解示例 - 图3

项目的文件/文件夹树

Struts2 HelloWorld 注解示例 - 图4

就是这个带有注解的 Struts2 helloworld 应用的朋友。 如果要下载本教程的源代码,请按照以下给定的下载链接进行操作。

源码下载

学习愉快!