Spring與Struts整合之讓Spring管理控制器操作示例
本文實(shí)例講述了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用來(lái)初始化Struts 2并且處理所有的WEB請(qǐng)求。 --> <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"> <!-- 定義一個(gè)業(yè)務(wù)邏輯組件,實(shí)現(xiàn)類(lèi)為MyServiceImp --> <bean id="myService" class="org.crazyit.app.service.impl.MyServiceImpl"/> <!-- 讓Spring管理的Action實(shí)例,并依賴(lài)注入業(yè)務(wù)邏輯組件 --> <bean id="loginAction" 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>登錄頁(yè)面</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="重設(shè)" 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>成功頁(yè)面</title> </head> <body> 您已經(jīng)登錄!<br/> <s:actionmessage /> </body> </html>
3 error.jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!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>錯(cuò)誤頁(yè)面</title> </head> <body> 您不能登錄! </body> </html>
四 Struts配置
<?xml version="1.0" encoding="GBK"?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <!-- Struts 2配置文件的根元素 --> <struts> <!-- 配置了系列常量 --> <constant name="struts.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true"/> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <package name="lee" extends="struts-default"> <!-- 定義處理用戶請(qǐng)求的Action,該Action的class屬性不是實(shí)際處理類(lèi) , 而是Spring容器中的Bean實(shí)例的ID --> <action name="login" class="loginAction"> <!-- 為兩個(gè)邏輯視圖配置視圖頁(yè)面 --> <result name="error">/WEB-INF/content/error.jsp</result> <result>/WEB-INF/content/welcome.jsp</result> </action> <!-- 讓用戶直接訪問(wèn)該應(yīng)用時(shí)列出所有視圖頁(yè)面 --> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
五 action
package org.crazyit.app.action; import com.opensymphony.xwork2.ActionSupport; import org.crazyit.app.service.*; public class LoginAction extends ActionSupport { // 下面是用于封裝用戶請(qǐng)求參數(shù)的兩個(gè)成員變量 private String username; private String password; // 系統(tǒng)所用的業(yè)務(wù)邏輯組件 private MyService ms; // 設(shè)值注入業(yè)務(wù)邏輯組件所必需的setter方法 public void setMs(MyService ms) { this.ms = ms; } // username的setter和getter方法 public void setUsername(String username) { this.username = username; } public String getUsername() { return this.username; } // password的setter和getter方法 public void setPassword(String password) { this.password = password; } public String getPassword() { return this.password; } // 處理用戶請(qǐng)求的execute方法 public String execute() throws Exception { // 調(diào)用業(yè)務(wù)邏輯組件的validLogin()方法 // 驗(yàn)證用戶輸入的用戶名和密碼是否正確 if (ms.validLogin(getUsername(), getPassword()) > 0) { addActionMessage("哈哈,整合成功!"); return SUCCESS; } return ERROR; } }
六 Service
1 接口
package org.crazyit.app.service; public interface MyService { int validLogin(String username , String pass); }
2 實(shí)現(xiàn)類(lèi)
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class MyServiceImpl implements MyService { public int validLogin(String username , String pass) { // 此處只是簡(jiǎn)單示范,故直接判斷用戶名、密碼是否符合要求 if ( username.equals("crazyit.org") && pass.equals("leegang") ) { return 99; } return -1; } }
七 測(cè)試
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- spring整合struts2過(guò)程詳解
- struts2+spring+ibatis框架整合實(shí)現(xiàn)增刪改查
- WebSocket整合SSM(Spring,Struts2,Maven)的實(shí)現(xiàn)示例
- Spring+Hibernate+Struts(SSH)框架整合實(shí)戰(zhàn)
- struts+spring+hibernate三個(gè)框架的整合
- Spring整合Struts2的兩種方法小結(jié)
- 詳解JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
- struts2.2.3+spring3.1.0+mybatis3.1.0框架整合集成簡(jiǎn)單demo
- SSH框架網(wǎng)上商城項(xiàng)目第1戰(zhàn)之整合Struts2、Hibernate4.3和Spring4.2
- struts2 spring整合fieldError問(wèn)題
- Spring與Struts整合之使用自動(dòng)裝配操作示例
相關(guān)文章
Maven3種打包方式中maven-assembly-plugin的使用詳解
這篇文章主要介紹了Maven3種打包方式中maven-assembly-plugin的使用,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過(guò)程詳解
這篇文章主要介紹了SpringMVC Mock測(cè)試實(shí)現(xiàn)原理及實(shí)現(xiàn)過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10淺談Spring Data Redis讀不到設(shè)進(jìn)去的值
本文主要介紹了Spring Data Redis怎么讀不到我剛才設(shè)進(jìn)去的值,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Java線程重復(fù)執(zhí)行以及操作共享變量的代碼示例
這篇文章主要介紹了Java中對(duì)線程重復(fù)執(zhí)行以及操作共享變量的代碼示例,來(lái)自于Java面試題目的練習(xí)整理,需要的朋友可以參考下2015-12-12IDEA2022版本創(chuàng)建maven?web項(xiàng)目的兩種方式詳解
創(chuàng)建maven?web項(xiàng)目有兩種方式,一種是使用骨架方式,一種是不使用骨架的方式,本文結(jié)合實(shí)例代碼給大家介紹了IDEA2022版本創(chuàng)建maven?web項(xiàng)目的兩種方式,需要的朋友可以參考下2023-02-02