跟着官网学Struts2使用(二)

有了上一节的成功尝试,我明显感觉信心倍增,自信满满,事不宜迟,继续加油咯

这一次,是动手实验篇,我偏重于在教程中找到重点信息,摘录备忘,以及记录整个实现过程;单词都是很简单的,加油

学习地址:http://struts.apache.org/2.2.1/docs/hello-world-using-struts-2.html

实现目标:Suppose you  want  to create a simple "Hello World" example that displays a welcome message.
    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
Step 1 - Create The Model Class  MessageStore.java
create the MessageStore class in src/main/java. Be sure to note the package statement below.(在项目目录下的src/main/目录下建立java文件夹,并且确保java文件的包结构正确)

package org.apache.struts.helloworld.model;

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.
Step 2 - Create The Action Class  HelloWorldAction.java
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.

package org.apache.struts.helloworld.action;

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;
	}
}
Step 3 - Create The View  HelloWorld.jsp
Create the below jsp in src/main/webapp for the  project.

<%@ 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>

Step 4 - Add The Struts Configuration In struts.xml

Edit the struts.xml file ( in the  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:

<%@ 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.

Step 6 - Build the WAR File and Run The Application

web容器中运行正常就成功咯

你可能感兴趣的:(跟着官网学Struts2使用(二))