本示例仅实现一个功能,即单击一个超链接,显示“hello world”。
步骤如下:
1 显然界面就是 view 层,用jsp实现
2 而数据保存在model层中,专门创建一个java类实现
3 control层贯通view层及model层,该层与view层和model层都有接口(接口是隐含在struts2框架内的),方便view与model交互。
4 如何通过view层,获得了model层中的数据——“hello world” 字符串,那就是通过struts2框架,这里需要配置struts.xml进行相关配置。
注意事项:
struts.xml 必须放在src文件夹下
存放第三方jar的lib和web.xml 放在WEN-INF文件夹下,jsp文件与WEN-INF在同一个目录下
具体代码:
jsp文件有两个,一个index.jsp 作为访问主页,一个HelloWorld.jsp,作为跳转页,即点击了index.jsp上的超链接,跳转到HelloWorld.jsp,这里的跳转操作不是html自身的跳转,而经过了struts2的框架。
index.jsp ,其中 taglib指定了s来表示struts-tag, <s:url action='hello' /> 定义了url,而且定义了url跳转调用的action,是hello,但是hello具体代表什么呢?别急,struts.xml中定义好了。
<%@ 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>
HelloWorld.jsp ,其中 <s:property value="messageStore.message"> 会调用MessageStore的GetMessage()方法,MessageStore就是我们要创建的Model层java类,我们拭目以待。
<%@ 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>Another Page....</h2>
<h2><s:property value="messageStore.message" /></h2>
</body>
</html>
MessageStore.java, 虽然该类并未继承任何类,
package org.apache.struts.helloworld.model;
public class MessageStore {
private String message;
public MessageStore() {
setMessage("Hello World!!!!");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
HelloWorldAction.java ,是控制层类,必须继承ActionSupport,get,set的方法及构造函数,必须给出,不然在上面的jsp中“messageStore.message” 就无法先调用getMessageStore(),再调用getMessage()函数了,这些隐藏的过程,都是struts2框架实现的。 execute() 在出发action时,会调用,同时返回一个参数SUCCESS
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;
}
}
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>
<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>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2Example</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>