Struts2笔记04——Hello World Example(转)

原文地址:https://www.tutorialspoint.com/struts_2/

 【注释】项目结构,次序估计有误

通过学习Struts2的架构,我们可以知道,在Struts2 web应用中,当你点击一个超链接、或者提交一个HTML表单的时候,用户的input将被传到Controller,之后Controller把input发送给叫做Action的Java class. 在Action被执行之后,Result会决定将哪个资源作为response的内容返回。这个资源通常是Jsp,当然也可以是一个PDF文件,又或者是个表格,或是Java applet窗口,等等。

假定你已经搭建好了开发所需要的环境。现在我们开始构建一个 Hello World struts2 project. 这个项目的目的是:创建一个web应用,可以输入用户的姓名,在用户提交后显示“Hello World” + “用户姓名”。下面4个组件是任何Struts2 project中都必须具备的:

SNComponents & Description
1 Action

Create an action class which will contain complete business logic and control the interaction between the user, the model, and the view.

2 Interceptors

Create interceptors if required, or use existing interceptors. This is part of Controller.

3 View

Create a JSPs to interact with the user to take input and to present the final messages.

4 Configuration Files

Create configuration files to couple the Action, View and Controllers. These files are struts.xml, web.xml, struts.properties.

我们打算使用Eclipse IDE,因此所有需要的组件都将被创建在一个动态Web工程下,所以让我们首先创建一个“Dynamic Web Project”。

创建一个 Dynamic Web Project:

启动你的Eclipse,然后依次打开 File > New > Dynamic Web Project 并且输入项目名“HelloWorldStruts2 ” ,其余选项不必改变(默认值就ok):

Hello World Sturts1

最后需要勾选 Generate Web.xml deployment descriptor 这个选项. 创建好的工程目录如下所示:

Hello World Sturts2

现在,把底下的这些文件从Struts2 lib文件夹复制到我们工程中的 WEB-INFlib 文件夹下:

  • commons-fileupload-x.y.z.jar

  • commons-io-x.y.z.jar

  • commons-lang-x.y.jar

  • commons-logging-x.y.z.jar

  • commons-logging-api-x.y.jar

  • freemarker-x.y.z.jar

  • javassist-.xy.z.GA

  • ognl-x.y.z.jar

  • struts2-core-x.y.z.jar

  • xwork-core.x.y.z.jar

创建Action类:

Action类是Struts2应用程序的核心,我们通过它实现大多数的业务逻辑。因此,我们首先在Java Resources > src 下创建一个HelloWorldAction.java,内容如下所示。

当用户点击一个URL时,由Action类响应用户操作。一个或者多个Action类的方法被执行,并且将返回一个String类型的result。基于这个result值,一个特定的JSP页面将被呈现。

package com.tutorialspoint.struts2;

public class HelloWorldAction{
   private String name;

   public String execute() throws Exception {
      return "success";
   }
   
   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
}

这是一个非常简单的class,仅有一个叫做“name”的属性,这个属性具备标准的 getters 和setter 以及一个execute()的方法。

你也可以将你的动作名直接作为你的类名,或者如下面内容所示,使用 struts.xml 文件映射到其他名字上。

创建View

我们需要一个JSP来呈现最终信息。WebContent/HelloWorld.jsp :

<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
   Hello World, <s:property value="name"/>
</body>
</html>

taglib指令告诉Servlet容器(JSP只不过是Servlet的另一种写法!)这个页面将使用Struts2标签,并且这些标签将以s开头。

s:property 标签显示Action类“name”属性的值,这个值是使用HelloWorldAction类的 getName() 方法返回的。

创建主界面:

这个JSP用作初始的action URL. 用户可以通过点击该页面中的submit去请求HelloWord.jsp视图,其结果就是促使Struts2框架去调用action hello

WebContent/index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Hello World</title>
</head>
<body>
   <h1>Hello World From Struts2</h1>
   <form action="hello">
      <label for="name">Please enter your name</label><br/>
      <input type="text" name="name"/>
      <input type="submit" value="Say Hello"/>
   </form>
</body>
</html>

hello action将通过struts2.xml映射到的HelloWorldAction,最终调用HelloWorldAction中的execute()方法,并获取它的返回值,以此决定呈现哪个视图。

配置文件

我们需要一个“映射”把URL、HelloWorldAction class (Model),以及HelloWorld.jsp (the view)联系在一起。

这个“映射”告诉Struts2框架哪个class将响应用户的action(也就是URL),class的哪个方法将会被执行,方法返回的result所对应的视图是哪个。

WebContent/WEB-INF/classes/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>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">
     
      <action name="hello" 
            class="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>
   </package>
</struts>

关于上面的配置文件,有几点要说明。

  1. 这里我们设定常数struts.devMode的值为,因为我们是在开发环境下工作,需要查看一些有用的日志消息。
  2. 然后,我们定义一个名为helloworld的数据包。当你想要把你的Actions集合在一起时,创建一个数据包是非常有用的。

在我们的示例中,action被命名为“hello”,当URL /hello.action被调用的时候,HelloWorldAction.class 中的execute方法将被执行,如果execute方法的返回结果为success,那么就把HelloWorld.jsp呈现给用户。

下一个步骤是是创建一个web应用部署描述文件,它是把请求传给Struts2的一个接入点。

我们在web.xml中定义一个oforg.apache.struts2.dispatcher.FilterDispatcher类的接入点。

WEB-INF/classes

<?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" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   id="WebApp_ID" version="3.0">
   
   <display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
   </welcome-file-list>
   <filter>
      <filter-name>struts2</filter-name>
      <filter-class>
         org.apache.struts2.dispatcher.FilterDispatcher
      </filter-class>
   </filter>

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

<url-pattern>/*</url-pattern>:在所有的URL上运行Struts2 过滤器。

启用详细日志:

 WEB-INF/classes/logging.properties

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = 
                              java.util.logging.ConsoleHandler

默认由logging.properties指定一个ConsoleHandler将日志记录按指定路线发送给stdout和FileHandler。程序运行日志的级别阈值可以使用SEVERE,WARNING,INFO,CONFIG,FINE,FINER,FINEST或者ALL。

这样,我们就准备好使用Struts2运行我们的Hello World程序了。

执行应用程序

把程序打包成war然后部署到tomcat下,输入URL:http://localhost:8080/HelloWorldStruts2/index.jsp

Hello World Struts4

输入Struts2,然后提交

Hello World Struts5

定义索引页面的方法(通过http://localhost:8080/HelloWorldStruts2/index.action可以访问):

<?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>
<constant name="struts.devMode" value="true" />
   <package name="helloworld" extends="struts-default">

      <action name="index">
            <result >/index.jsp</result>
      </action>

      <action name="hello" 
            class="com.tutorialspoint.struts2.HelloWorldAction" 
            method="execute">
            <result name="success">/HelloWorld.jsp</result>
      </action>

   </package>
</struts>
原文地址:https://www.cnblogs.com/xkxf/p/6986481.html