`

HelloWrod For struts2

    博客分类:
  • J2EE
 
阅读更多

Create a "Hello World" struts2 example, you need to do four things:

 

1. Create a class to store the welcome message (the model)

2. Create a server page to present the message (the view)

3. Create an Action class to control the interaction between the user, the model, and the view (the 

    controller)

4. Create a mapping (struts.xml) to couple the Action class and view

     By creating these components, we are separating the work flow into three well-known concerns: the View, the Model, and the Controller. Separating concerns makes it easier to manage applications as they become more complex.

 

 

Step 1 - Create The Model Class MessageStore.java

 

public class MessageStore {

private String message;

public MessageStore() {

setMessage("Hello Struts User");

}

 

public String getMessage() {

 

return message;

}

 

public void setMessage(String message) {

 

this.message = message;

}

 

}

 

       In the model class above note the use of public set and get methods to allow access to the private message String attribute. The Struts 2 framework requires that objects you want to expose to the view (HelloWorld.jsp) follow the JavaBean-style conventions.

 

 

Step 2 - Create The Action Class HelloWorldAction.java

 

       We need an Action class to act as the Controller. The Action class responds to a user action (in this example that action will be clicking an HTML hyperlink and sending a specific URL to the Servlet 

container). One or more of the Action class's methods are executed and a String result is returned. 

Based on the value of the result, a specific view page (in this example that view page is HelloWorld.jsp) is rendered.Note the package and import statements below.

 

 

import org.apache.struts.helloworld.model.MessageStore;

import com.opensymphony.xwork2.ActionSupport;

 

public class HelloWorldAction extends ActionSupport {

 

private static final long serialVersionUID = 1L;

 

private MessageStore messageStore;

public String execute() throws Exception {

messageStore = new MessageStore() ;

return SUCCESS;

}

 

public MessageStore getMessageStore() {

return messageStore;

}

 

public void setMessageStore(MessageStore messageStore) {

this.messageStore = messageStore;

}

 

}

 

 

       The Struts 2 framework will create an object of the HelloWorldAction class and call the execute method in response to a user's action (clicking on a hyperlink that sends a specific URL to the Servlet 

container).In this example, the execute method creates an object of class MessageStore and then returns the String constant SUCCESS.

 

       Note also the public set and get methods for the private MessageStore object. Since we want to make the MessageStore object available to the view page

 

Step 3 - Create The View HelloWorld.jsp

 

       We need a server page to present the message that is stored in the model class MessageStore. Create the below jsp in the WebContent folder (for the Ant project) and in src/main/webapp for the Mvn project).

 

HelloWorld.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>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Hello World!</title>

</head>

<body>

    <h2><s:property value="messageStore.message" /></h2>

</body>

</html>

 

     The taglib directive tells the Servlet container that this page will be using the Struts 2 tags and that these tags will be preceded by s.The s:property tag displays the value returned by calling the method getMessageStore of the HelloWorldAction controller class. That method returns a MessageStore object. By adding the .message onto the messageStore part of the value attribute we are telling the Struts 2 framework to then call the getMessage method of that MessageStore object. The getMessage method of class MessageStore returns a String. It is that String that will be displayed by the s:property tag.

 

 

 

Step 4 - Add The Struts Configuration In struts.xml

 

 

      We need a mapping to tie the URL, the HelloWorldAction class (controller), and 

the HelloWorld.jsp (the view) together. The mapping tells the Struts 2 framework which class will 

respond to the user's action (the URL), which method of that class will be executed, and what view to 

render based on the String result that method returns.Edit the struts.xml file (in the Ant project that file is in the src folder and in the Mvn project that file is in the src/main/resources folder) to add the action mapping. Place the action node (action name="hello") between the opening and closing package node, just after the action mapping with the name="index". Your complete struts.xml should look like:

 

<?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="basicstruts2" extends="struts-default">

 

 

<action name="index">

<result>/index.jsp</result>

</action>

<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" 

method="execute">

                        <result name="success">/HelloWorld.jsp</result>

</action>

 

</package>

 

</struts>

 

Step 5 - Create The URL Action

 

 

        In index.jsp (see WebContent folder for Ant project and src/main/webapp for Mvn project) let's add an Action URL the user can click on to tell the Struts 2 framework to run the execute method of the HelloWorldAction class and render the HelloWorld.jsp view.First add the taglib directive at the top of the jsp <%@ taglib prefix="s" uri="/struts-tags" %>. Next add this p tag <p><a href="<s:url action='hello'/>">Hello World</a></p> after the h1 tag. Your new index.jsp should look like:

 

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>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Basic Struts 2 Application - Welcome</title>

</head>

<body>

<h1>Welcome To Struts 2!</h1>

<p><a href="<s:url action='hello'/>">Hello World</a></p>

</body>

</html>

 

      The Struts url tag creates the URL with an action of hello. The hello action was mapped to the 

HelloWorldAction class and its execute method. When the user clicks on the above URL it will cause the Struts 2 framework to run the execute method of the HelloWorldAction class. After that method returns the String success, the view page HelloWorld.jsp will be rendered.

 

 

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Hello_World_Struts2_Ant</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

    </filter>

 

     <filter-mapping>

        <filter-name>struts2</filter-name>

        <url-pattern>/*</url-pattern>

    </filter-mapping>

 

</web-app>

 

该web配置表上,发送到该项目的所有请求,都是经过该struts过滤器处理


分享到:
评论

相关推荐

    jar for struts2 basic

    "jar for struts2 basic"指的是与Struts2基本功能相关的JAR库文件集合,这些文件是构建和运行Struts2应用的基础。在Java Web开发中,JAR(Java Archive)文件是用于打包类、资源和元数据的容器,便于代码重用和部署...

    在struts2中要实现for循环

    在Struts2框架中实现For循环是一个非常实用的功能,它可以帮助开发者在页面上展示一系列的数据或者执行特定次数的操作。下面将详细介绍如何在Struts2中实现For循环,并且会覆盖到Struts2的相关配置、使用场景以及...

    Struts2漏洞检查工具Struts2.2019.V2.3

    Struts2是一款非常流行的Java Web框架,用于构建企业级应用。然而,随着时间的推移,Struts2在安全方面暴露出了一些重要的漏洞,这给使用该框架的系统带来了潜在的安全风险。"Struts2漏洞检查工具Struts2.2019.V2.3...

    struts2jar包

    Struts2是一个强大的Java EE应用程序框架,主要用于构建企业级的Web应用。它的核心是MVC(Model-View-Controller)设计模式,可以帮助开发者组织代码,提高开发效率,并且提供了丰富的特性来支持表单验证、国际化、...

    struts2-core.jar

    struts2-core-2.0.1.jar, struts2-core-2.0.11.1.jar, struts2-core-2.0.11.2.jar, struts2-core-2.0.11.jar, struts2-core-2.0.12.jar, struts2-core-2.0.14.jar, struts2-core-2.0.5.jar, struts2-core-2.0.6.jar,...

    Struts2漏洞测试

    Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试Struts2漏洞测试...

    Struts2接口文档

    Struts2是一个强大的Java web应用程序开发框架,它基于Model-View-Controller(MVC)设计模式,旨在简化创建用户交互式、数据驱动的web应用的过程。这个“Struts2接口文档”是开发者的重要参考资料,提供了关于...

    struts2 总结工程大全

    struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全struts2 总结工程大全...

    Struts2视频教程

    ### Struts2核心知识点解析 #### 一、Struts2框架概述 - **定义与特点**:Struts2是一款基于MVC(Model-View-Controller)设计模式的Java Web应用程序框架,它继承了Struts1的优点,同时在设计上更加灵活、易用,...

    struts2项目开发

    Struts2 项目开发 Struts2 是一个基于 Java Web 的框架,广泛应用于 Web 应用程序的开发。下面将从 Struts2 项目开发的角度,详细介绍 Struts2 框架的应用、开发流程、技术架构、实践经验等方面的知识点。 项目...

    Struts2VulsTools-Struts2系列漏洞检查工具

    该工具的打开路径为:\Struts2VulsTools-2.3.20190927\Test\bin\Release\Text.exe 2019-09-25: 优化部分EXP在部分情况下被WAF拦截的问题,提高检测成功率,优化自定义上传路径exp,文件所在目录不存在时自动创建...

    Struts2教学视频

    Struts2是一个强大的Java web开发框架,用于构建可维护、可扩展且结构良好的应用程序。它在MVC(Model-View-Controller)设计模式的基础上提供了一种实现方式,使得开发者能够更方便地处理用户请求,控制业务逻辑,...

    struts2所有jar包程序文件

    Struts2是一个非常著名的Java Web开发框架,由Apache软件基金会维护。它基于MVC(Model-View-Controller)设计模式,极大地简化了构建基于Java EE的Web应用程序的过程。本资源包含"struts2所有jar包程序文件",是...

    struts2 最新漏洞 S2-016、S2-017修补方案 .docx

    Struts2 最新漏洞 S2-016、S2-017 修补方案 Struts2 是一个基于 Java 的 Web 应用程序框架,由 Apache 软件基金会维护。最近,Struts2 发生了两个严重的漏洞,分别是 S2-016 和 S2-017,这两个漏洞可能会导致攻击者...

    struts2 chm 帮助文档

    struts2 chm 程序包 org.apache.struts2 接口概要 接口 说明 StrutsStatics Constants used by Struts. 类概要 类 说明 RequestUtils Request handling utility class. ServletActionContext Web-specific ...

    struts2-showcase.rar

    Struts2-showcase是一个用于演示和学习Apache Struts2框架功能的开源项目。这个压缩包“struts2-showcase.rar”包含了完整的源代码,旨在帮助开发者深入理解Struts2框架的工作原理及其各种特性。以下是对Struts2和...

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板

    struts2环境搭建+struts2 新闻发布系统+struts2 留言板 struts2环境搭建:基础框架搭建,简单易上手,适合新手,比你看书强多了,实践源于理论而高于理论,贵在实践 朋友。 struts2 新闻发布系统:struts2+jsp 功能不...

    一个struts2的例子:彻底解决STRUTS2 错误There is no Action mapped for namespace / and action name login

    前几天在网上下载一个struts2的helloword的例子,那个作者也真够缺德的,搞个错误的程序,害得我查了一天的程序错误。 最后发现竟然是struts.xml被写成啦sturts.xml。 碰见这样的问题先鄙视下提供例子的作者, 再...

    全网最全Struts 2 全版本漏洞检测工具,最新struts漏洞更新

    Struts 2是一款基于Java的开源MVC框架,它在Web应用开发中广泛使用,但同时也因其复杂的架构和历史遗留问题,成为了网络安全的焦点。这个标题提到的是一个全面的Struts 2漏洞检测工具,旨在帮助开发者和安全专家识别...

    DW插件for struts

    2. **智能提示与补全**:在编辑Struts相关代码时,插件提供智能提示功能,包括Action、Form属性、JSP标签等,有助于减少错误并提高编码准确性。 3. **集成调试**:DW插件 for Struts 可能集成调试功能,允许开发者...

Global site tag (gtag.js) - Google Analytics