Address: http://blog.csdn.net/sushengmiyan/article/details/40349201
Official documents: http://struts.apache.org/release/2.3.x/docs/hello-world-using-struts-2.html
Sushengmiyan
Certificate ------------------------------------------------------------------------------------------------------------------------------------
When you click a hyperlink in the struts 2 web application or submit a form data, the input data is not transferred to another page on the server, but to a Java class you provide. these classes are called actions. when an action is fired, a resource is selected to display the response. generally, a resource is a webpage, a PDF file, an Excel tab, or a Java Applet window.
Suppose you want to create a simple "Hello World" example to show the welcome information. after setting up the struts work environment (refer to how to create a struts 2 web application) and creating a "hello World" example, you need to do the following four things:
- Create a Java class (model) that stores welcome information)
- Create a page for displaying information (view)
- Create an action class to control the relationship between users, models, and views (Controller)
- Create a mapping (struts. XML) to combine action classes and views.
This article assumes that you have completed how to create a struts 2 web application experience and have a basic struts workspace. the source code for this helloworld experience can be downloaded from the struts 2 GitHub repository https://github.com/apache/struts-examples. the example project uses Maven to manage dependencies and structures. war file.
Step 1-create a model class messagestore. Java
In our first example, we create a new package com. susheng. struts2maven. Bean and create a messagestore class in it. The content is as follows:
package com.susheng.struts2Maven.bean;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; }}
Step 2-create action class helloworldaction. Java
Create a package com. susheng. struts2maven. Action. Under this package, create a class helloworldaction. The content is as follows:
package com.susheng.struts2Maven.action;import com.opensymphony.xwork2.ActionSupport;import com.susheng.struts2Maven.bean.MessageStore;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 view helloworld. jsp
The content is as follows:
<%@ 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">
Step 4-add struts configuration to struts. xml
The complete content is as follows:
<?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="com.susheng.struts2Maven.action.HelloWorldAction" method="execute"> <result name="success">/HelloWorld.jsp</result> </action> </package> </struts>
Step 5-create a URL action
Modify index. jsp to the following:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Step 6-construct the war file and run the program. The war package I constructed is basic_struts.war, so my access link is http: // localhost: 8080/basic_struts/index. action. Click the helloworld link.
The second example is completed.
How struts2 works:
[Struts2 Study Notes] section 3 six steps required to create struts 2 helloworld