本教程基于netbeans6.9.1,tomcat是netbeans自带的6.0.26,已经经过测试。
1 新建java
web项目,在WEB-INF下新建classes文件夹和lib文件夹。在struts框架的库里找到如下所示的库文件放入lib下如图
在classes文件夹下新建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="struts2" namespace="/" extends="struts-default"> </package> <!-- Add packages here --> </struts>
2 修改web.xml文件,假如如下内容
<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>
3 在web的根目录下的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>
4 新建java包,org.ynu.action 和org.ynu.model。在model包里新建
MessageStore类如下
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;
}
}
在action包里新建HelloWorldAction类代码如下
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;
}
}
5 在web更目录下创建一个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>
6 修改struts.xml代码如下,加入action到package节点下
<?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.ynu.HelloWorldAction" method="execute"> <result name="success">/helloworld.jsp</result> </action> </package> </struts>
运行index.jsp然后点击hello链接,如果出现了Hello Struts User,则说明成功了。
代码的运行原理:
1 web.xml中的org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter负责拦截所有的来自客户端浏览器的请求,包括.jsp .action .do等,由 <url-pattern>/*</url-pattern>配置决定的
2 当点击Hello World链接时,struts2框架在struts.xml文件中查找hello这个action对应的类,这里是action包中的HelloWorldAction 类,然后此次请求就交给了这个类来完成,负责完成的方法是execute
3 execute方法创建了一个MessageStore 对象,然后返回一个SUCCESS,框架检查这个action对应映射看如果SUCCESS返回将转发到哪一个jsp页面,这里是helloworld.jsp
4 helloworld页面中的<s:property value="messageStore.message" />表示要调用hello这个action对应的类HelloWorldAction中的getMessageStore 方法,取得messageObject对象,并且该对象调用其中的getMessage 方法取得message。字段的命名方式和存取方式要符合javabean的命名规范
5 呈现结果