MyBatis 3章 MyBatis Spring Struts2 整合應用
1、技術目標
- 爲項目添加Struts2框架
- 整合Spring與strtus2
- 爲項目添加jsp頁面,操作影片CRUD
注意:關於strtus2框架其他方面的應用細節不在本文討論範圍
2、使用準備
2.1) 在項目(Web)中新增如下jar包,struts版本2.2.1.1(本文已提供下載):
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.1.jar
freemarker-2.3.16.jar
javassist-3.12.0.GA.jar
ognl-3.0.jar
struts2-core-2.2.1.1.jar
struts2-spring-plugin-2.2.1.1.jar
xwork-core-2.2.1.1.jar
2.2)創建如下包,放置struts控制器(Action)代碼:
com.xxx.web.struts.action
2.3)在src下創建Spring配置文件applicationContext-actions.xml ,內容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 這裏配置Action --> </beans>
2.4)在src下創建struts2配置文件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.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.objectFactory.spring.autoWire" value="type" /> <constant name="struts.ui.theme" value="simple"></constant> <package name="film" namespace="/film" extends="struts-default"> <!-- 設置Action --> </package> </struts>
2.5)web.xml 中配置struts2
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> </context-param> <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> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
3、在com.xxx.web.struts.action包下創建類FilmAction,如下:
package com.xxx.web.struts.action;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xxx.pojo.Film;
import com.xxx.service.FilmService;
@SuppressWarnings("serial")
public class FilmAction extends ActionSupport implements ModelDriven<Film> {
//業務邏輯對象
@Autowired
private FilmService filmService;
//封裝查詢結果
private List<Film> filmList;
//封裝頁面提交的表單數據
private Film film = new Film();
/**
* 獲取所有的電影
* @return
* @throws Exception
*/
public String findFilm() throws Exception {
this.filmList = this.filmService.getAllFilm();
return SUCCESS;
}
/**
* 根據影片ID獲取影片信息
* @return
* @throws Exception
*/
public String detailFilm() throws Exception {
int id = film.getId().intValue();
Film film = this.filmService.getFilmById(id);
this.film.setFname(film.getFname());
return SUCCESS;
}
/**
* 添加影片
* @return
* @throws Exception
*/
public String insertFilm() throws Exception {
this.filmService.insertFilm(film);
return SUCCESS;
}
/**
* 修改影片
* @return
* @throws Exception
*/
public String updateFilm() throws Exception {
this.filmService.updateFilm(film);
return SUCCESS;
}
/**
* 刪除影片
* @return
* @throws Exception
*/
public String deleteFilm() throws Exception {
int id = film.getId().intValue();
this.filmService.deleteFilm(id);
return SUCCESS;
}
public Film getModel() {
return film;
}
public List<Film> getFilmList() {
return filmList;
}
public void setFilmList(List<Film> filmList) {
this.filmList = filmList;
}
}
4、在applicationContext-actions.xml中配置FilmAction
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 創建FilmAction --> <bean id="filmAction" class="com.xxx.web.struts.action.FilmAction" scope="prototype"/> </beans>
5、在struts.xml中配置Action
<?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.enable.DynamicMethodInvocation" value="true" /> <constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" /> <constant name="struts.objectFactory" value="spring" /> <constant name="struts.objectFactory.spring.autoWire" value="type" /> <constant name="struts.ui.theme" value="simple"></constant> <package name="film" namespace="/film" extends="struts-default"> <!-- 獲取所有影片 --> <action name="findFilm" class="filmAction" method="findFilm"> <result name="success">/manager/films.jsp</result> </action> <!-- 添加影片 --> <action name="insertFilm" class="filmAction" method="insertFilm"> <result name="success" type="redirectAction">findFilm</result> </action> <!-- 獲取影片詳情 --> <action name="detailFilm" class="filmAction" method="detailFilm"> <result name="success">/manager/updateFilm.jsp</result> </action> <!-- 修改影片 --> <action name="updateFilm" class="filmAction" method="updateFilm"> <result name="success" type="redirectAction">findFilm</result> </action> <!-- 刪除影片 --> <action name="deleteFilm" class="filmAction" method="deleteFilm"> <result name="success" type="redirectAction">findFilm</result> </action> </package> </struts>
6、在WebRoot(頁面路徑下)創建文件夾manager,在manager下創建3個jsp文件:
- films.jsp (查詢頁面)
- insertFilm.jsp (添加影片頁面)
- updateFilm.jsp (修改影片頁面)
6.1)films.jsp代碼如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>信息操作</title>
</head>
<body>
<s:form action="/film/findFilm" method="post">
<s:submit value=" 獲取所有影片信息 "></s:submit>
</s:form>
<a href="<%=basePath %>manager/insertFilm.jsp">添加影片信息</a><br />
<s:if test="filmList != null">
<table border="1" width="40%">
<tr>
<th>序號</th><th>影片名</th><th>操作</th>
</tr>
<%-- 遍歷影片信息 --%>
<s:iterator var="film" value="filmList" status="st">
<tr>
<td><s:property value="#st.index+1" /></td>
<td><s:property value="fname" /></td>
<td>
<s:url id="detailUrl" value="/film/detailFilm">
<s:param name="id" value="%{id}"/>
</s:url>
<s:a href="%{detailUrl}">[修改]</s:a>
<s:url id="deleteUrl" value="/film/deleteFilm">
<s:param name="id" value="%{id}"/>
</s:url>
<s:a href="%{deleteUrl}">[刪除]</s:a>
</td>
</tr>
</s:iterator>
</table>
</s:if>
</body>
</html>
6.2)insertFilm.jsp代碼如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>添加影片</title>
</head>
<body>
<s:form action="/film/insertFilm" method="post">
<s:textfield name="fname" value="" />
<s:submit value=" 添加 "></s:submit>
</s:form>
</body>
</html>
6.3)updateFilm.jsp代碼如下:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" %>
<%@taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>修改影片</title>
</head>
<body>
<s:form action="/film/updateFilm" method="post">
<s:hidden name="id" />
<s:textfield name="fname" />
<s:submit value=" 修改 "></s:submit>
</s:form>
</body>
</html>
提示:本文示例項目已提供下載