HelloWrod For struts2

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过滤器处理


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值