本篇文章給大家分享的是有關(guān)怎么在Spring中使用Struts實現(xiàn)自動裝配,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
創(chuàng)新互聯(lián)專注于勐臘網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供勐臘營銷型網(wǎng)站建設(shè),勐臘網(wǎng)站制作、勐臘網(wǎng)頁設(shè)計、勐臘網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造勐臘網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供勐臘網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
一 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定義一個業(yè)務(wù)邏輯組件,實現(xiàn)類為MyServiceImp, 此處的id必須與Action的setter方法名對應(yīng) --> <bean id="ms" class="org.crazyit.app.service.impl.MyServiceImpl"/> </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> <h4>用戶登錄</h4> <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>成功頁面</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>錯誤頁面</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> <!-- 配置了系列常量 --> <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"> <!-- 定義處理用戶請求的Action --> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 為兩個邏輯視圖配置視圖頁面 --> <result name="error">/WEB-INF/content/error.jsp</result> <result>/WEB-INF/content/welcome.jsp</result> </action> <!-- 讓用戶直接訪問該應(yīng)用時列出所有視圖頁面 --> <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 { // 下面是用于封裝用戶請求參數(shù)的兩個成員變量 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; } // 處理用戶請求的execute方法 public String execute() throws Exception { // 調(diào)用業(yè)務(wù)邏輯組件的validLogin()方法 // 驗證用戶輸入的用戶名和密碼是否正確 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 實現(xiàn)類
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class MyServiceImpl implements MyService { public int validLogin(String username , String pass) { // 此處只是簡單示范,故直接判斷用戶名、密碼是否符合要求 if ( username.equals("crazyit.org") && pass.equals("leegang") ) { return 99; } return -1; } }
七 測試
以上就是怎么在Spring中使用Struts實現(xiàn)自動裝配,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文名稱:怎么在Spring中使用Struts實現(xiàn)自動裝配
文章起源:http://chinadenli.net/article16/gogcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、虛擬主機、軟件開發(fā)、網(wǎng)頁設(shè)計公司、企業(yè)建站、品牌網(wǎng)站設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)