本文實例講述了Spring與Struts整合之讓Spring管理控制器操作。分享給大家供大家參考,具體如下:
一 Web配置
<?xml version="1.0" encoding="GBK"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 使用ContextLoaderListener初始化Spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 定義Struts 2的FilterDispathcer的Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- FilterDispatcher用來初始化Struts 2并且處理所有的WEB請求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
二 applicationContext.xml配置
<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定義一個業務邏輯組件,實現類為MyServiceImp --> <bean class="org.crazyit.app.service.impl.MyServiceImpl"/> <!-- 讓Spring管理的Action實例,并依賴注入業務邏輯組件 --> <bean class="org.crazyit.app.action.LoginAction" scope="prototype" p:ms-ref="myService"/></beans>
三 視圖
1 loginForm.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>登錄頁面</title></head><body><h3>用戶登錄</h3><s:form action="login"> <s:textfield name="username" label="用戶名"/> <s:textfield name="password" label="密碼"/> <tr align="center"> <td colspan="2"> <s:submit value="登錄" theme="simple"/> <s:reset value="重設" theme="simple"/> </td> </tr></s:form></body></html>
2 welcome.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %><%@taglib prefix="s" uri="/struts-tags"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>成功頁面</title></head><body> 您已經登錄!<br/> <s:actionmessage /></body></html>
新聞熱點
疑難解答