1、步骤:
① Create a class to store the welcome message (the model)
-
MessageStore.java
<div style="text-align: left;"><pre name="code" class="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;
}
}
② Create a server page to present the message (the view)
HelloWorldAction.java
<span style="font-family: Verdana, arial, sans-serif;font-size:18px; line-height: 16px; text-align: center; ">package org.apache.struts.helloworld.action;</span>
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;
}
}
③ Create an Action class to control the interaction between the user, the model, and the view (the controller)
HelloWorld.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>注: <s:property value="messageStore.message" />是用过MessageStore对象的getMessage()方法获取message属性值的。
<!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>
④ Create a mapping (struts.xml) to couple the Action class and view
struts.xml
<span style="font-family: Verdana, arial, sans-serif;font-size:18px; line-height: 16px; text-align: center; "><?xml version="1.0" encoding="UTF-8"?></span>
<!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>
注:
①不写class默认是ActionSupport类 ,在<result>中不写name则name默认值为success
index.jsp
②
<constant name="struts.devMode" value="true" /> struts.devMode常量代表是否是开发模式,如果是开发模式则使用热部署,structs2默认不使用开发模式
<%@ 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>
<package name="basicstruts2" namespace="/aa/bb" extends="struts-default">
<action name="hello" class="com.action.HelloWorldAction" method="execute">
<result name="success">/helloworld.jsp</result>
</action>
</package>
命名空间namespace="/aa/bb",所以在访问是路径将是/aa/bb/helloworld.action
在jsp中<p><a href="<s:url namespace="/aa/bb" action='hello'/>">Hello World</a></p>加入namespace="/aa/bb"也是可以的
3、配置后缀名:
<constant name="struts.action.extension" value="action,,do" />
后缀名为.action,空,.do的。虽然很多配置都在structs,core中的default.properties文件中,但是尽量不去修改,以免引入错误,如果需要修改,就需要重新打成jar包,再引入进来。