完成後的結構
1. 準備工作:配置 tw.edu.kh.action 跟 te.edu.kh.model 的 package, 配置 src/main/resources 的 package folder
2. 配置 src/main/resources/struts.xml
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>
<package name="basicStruts" extends="struts-default">
<action name="hello" class="tw.edu.kh.action.HelloWorldAction" method="execute">
<result name="success">/HelloWorld.jsp</result>
</action>
</package>
</struts>
3. 配置 tw.edu.kh.action.HelloWorldAction
Struts 2 Action classes usually extend the ActionSupport class
HELLOWORLDACTION.JAVA
package tw.edu.kh.action;
import com.opensymphony.xwork2.ActionSupport;
import tw.edu.kh.model.MessageStore;
public class HelloWorldAction extends ActionSupport {
private static final long serialVersionUID = 1L;
public String execute() throws Exception {
messageStore = new MessageStore() ;
if (userName != null) {
messageStore.setMessage( messageStore.getMessage() + " " + userName);
}
helloCount++;
return SUCCESS;
}
private MessageStore messageStore;
public MessageStore getMessageStore() {
return messageStore;
}
public void setMessageStore(MessageStore messageStore) {
this.messageStore = messageStore;
}
private static int helloCount = 0;
public int getHelloCount() {
return helloCount;
}
public void setHelloCount(int a) {
HelloWorldAction.helloCount = a;
}
private String userName;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
4. 配置 tw.edu.kh.model.MessageStore
MESSAGESTORE.JAVA
package tw.edu.kh.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;
}
public String toString() {
return message + " (from toString)";
}
}
5. 配置 src/main/webapp/index.jsp
INDEX.JSP
<%@ page contentType="text/html" 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Basic Struts 2 Application - Welcome</title>
<style>
.fieldset-auto-width { display: inline-block; }
</style>
</head>
<body>
<h1>Welcome To Struts 2!</h1>
<div>
<s:form action="hello">
<s:textfield name="userName" label="Your name" />
<s:submit value="Submit" />
</s:form>
</div>
</body>
</html>
6. 配置 src/main/webapp/HelloWorldAction.jsp
HELLOWORLD.JSP
<%@ 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World!</title>
<style>
.fieldset-auto-width { display: inline-block; }
</style>
</head>
<body>
<s:property value="messageStore.message" />
</body>
</html>
7. 配置 src/main/webapp/WEB-INF/web.xml
WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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>strutsTutorials</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<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>
8. Run As Maven Build