?
display-name元素提供GUI工具可能會用來標記這個特定的Web應(yīng)用的一個名稱
?
description元素給出與此有關(guān)的說明性文本
?
context-param元素聲明應(yīng)用范圍內(nèi)的初始化參數(shù)。
?
filter?過濾器元素將一個名字與一個實現(xiàn)javax.servlet.Filter接口的類相關(guān)聯(lián)
?
filter-mapping?命名一個過濾器后,就要利用filter-mapping元素把它與一個或多個servlet或JSP頁面相關(guān)聯(lián)。
?
listener?servlet?API的版本2.3增加了對事件監(jiān)聽程序的支持,事件監(jiān)聽程序在建立、修改和刪除會話或????????? ?servlet環(huán)境時得到通知。Listener元素指出事件監(jiān)聽程序類。
?
servlet?在向servlet或JSP頁面制定初始化參數(shù)或定制URL時,必須首先命名servlet或JSP頁面。Servlet元素就是用來完成此項任務(wù)的。
servlet-mapping?服務(wù)器一般為servlet提供一個缺省的URL:
http://host/webAppPrefix/servlet/ServletName。但是,常常會更改這個URL,以便servlet可以訪問初始化參數(shù)或更容易地處理相對URL。在更改缺省URL時,使用servlet-mapping元素。
session-config?如果某個會話在一定時間內(nèi)未被訪問,服務(wù)器可以拋棄它以節(jié)省內(nèi)存。可通過使用HttpSession的setMaxInactiveInterval方法?明確設(shè)置單個會話對象的超時值,或者可利用session-config元素制定缺省超時值。
mime-mapping?如果Web應(yīng)用具有想到特殊的文件,希望能保證給他們分配特定的MIME類型,則mime-mapping元素提供這種保證。
welcome-file-list元素指示服務(wù)器在收到引用一個目錄名而不是文件名的URL時,使用哪個文件。
error-page元素使得在返回特定HTTP狀態(tài)代碼時,或者特定類型的異常被拋出時,能夠制定將要顯示的頁面。
taglib元素對標記庫描述符文件(Tag?Libraryu?Descriptor?file)指定別名。此功能使你能夠更改TLD文件的位置,而不用編輯使用這些文件的JSP頁面。
resource-env-ref 元素聲明與資源相關(guān)的一個管理對象。
resource-ref 元素聲明一個資源工廠使用的外部資源。
security-constraint 元素制定應(yīng)該保護的URL。它與login-config元素聯(lián)合使用
login-config元素來指定服務(wù)器應(yīng)該怎樣給試圖訪問受保護頁面的用戶授權(quán)。它與sercurity-constraint元素聯(lián)合使用。
security-role 元素給出安全角色的一個列表,這些角色將出現(xiàn)在servlet元素內(nèi)的security-role-ref元素的role-name子元素中。分別地聲明角色可使高級IDE處理安全信息更為容易。
env-entry 元素聲明Web應(yīng)用的環(huán)境項。
ejb-ref 元素聲明一個EJB的主目錄的引用。
ejb-local-ref 元素聲明一個EJB的本地主目錄的應(yīng)用。
一般的MVC框架處理的問題包括
1.將web頁面中的輸入元素封裝成為一個(請求)數(shù)據(jù)對象。
2.根據(jù)請求不同調(diào)用相應(yīng)的邏輯處理單元,并將(請求)數(shù)據(jù)對象作為參數(shù)傳入。
3.邏輯處理單元完成運算后,返回一個結(jié)果數(shù)據(jù)對象。
4.將結(jié)果數(shù)據(jù)對象中的數(shù)據(jù)與預(yù)先設(shè)計好的表現(xiàn)層相融合并展現(xiàn)給用戶。
1.介紹一下junit
???junit是一個用來單元測試的工具,它可以針對一個/多個類的單個或多個方法進行測試,還可以自動化套件測試.將junit.jar包從www.junit.org.載下來放到eclipse 項目中的java build path中.
2.創(chuàng)建一個TestCase
???File > New > JUnit Test Case 或者點擊"
"來創(chuàng)建一個TestCase
???代碼如下
???import junit.framework.TestCase;
???public class SampleTest extends TestCase {
???? private java.util.List emptyList;
???? /**
????? * Sets up the test fixture.
????? * (Called before every test case method.)
????? */
???? protected void setUp() {
????????? emptyList = new java.util.ArrayList();
???? }?
???? /**
????? * Tears down the test fixture.
????? * (Called after every test case method.)
????? */
???? protected void tearDown() {
????????? emptyList = null;
???? }?
???? public void testSomeBehavior() {
????????? assertEquals("Empty list should have 0 elements", 0, emptyList.size());
???? }?
???? public void testForException() {
????????? try {
?????????????? Object o = emptyList.get(0);
?????????????? fail("Should raise an IndexOutOfBoundsException");
????????? }
????????? catch (IndexOutOfBoundsException success) {
????????? }
???? }
}
這個例子有兩個方法需要測試,第一個方法測試list中沒有任何對象,第二個方法測試沒有使用斷言,它一定會成功
3.創(chuàng)建一個TestSuite
???通過測試套件可以運行多個測試用例
?? (3.1)選擇???File > New > Other... > Java > JUnit > JUnit Test Suite. 或者
Other... > Java > JUnit > JUnit Test Suite,
import junit.framework.Test;
import junit.framework.TestSuite;
???public class AllTests {
????public static Test suite() {
????????TestSuite suite = new TestSuite("Test for com.xu.Test");
????????//$JUnit-BEGIN$
????????suite.addTestSuite(SampleTest .class);
????????//$JUnit-END$
????????return suite;
????}
}
Inversion
of Control Containers(IOC)由容器來控制程序間的關(guān)系,而非程序來控制,就是所謂的容器控制反轉(zhuǎn)。
Dependency Injection 容器動態(tài)的將某種動態(tài)關(guān)系注入到主鍵當中。
依賴注入的幾種實現(xiàn)
???type1 接口注入
????????????public class ClassA {
??????????????????private InterfaceB clzB;
??????????????????public doSomething() {
????????????????????????Ojbect obj =
??????????????????????????????Class.forName(Config.BImplementation).newInstance();
?????????????????????????clzB = (InterfaceB)obj;
?????????????????????????clzB.doIt()
??????????????????}
????????????……
????????????}
???我們通過配置文件動態(tài)的加載類,然后強制轉(zhuǎn)換成相應(yīng)的類來實現(xiàn)動態(tài)注入,對于IOC容器來說加載類的過程由容器來完成。
web容器采用type1 接口注入的具體應(yīng)用
public class MyServlet extends HttpServlet {
public void doGet(
?????????HttpServletRequest request,
?????????HttpServletResponse response)
????????????throws ServletException, IOException {
??????……
???}
}
Type2 設(shè)值注入? 它由容器通過Setter方法完成依賴關(guān)系的設(shè)置
Type3構(gòu)造了注入?
public class DIByConstructor {
???private final DataSource dataSource;
???private final String message;
???public DIByConstructor(DataSource ds, String msg) {
??????this.dataSource = ds;
??????this.message = msg;
???}
???……
}
Bean Wrapper
???? 由容器將依賴關(guān)系注入到組件當中,在運行期間由spring根據(jù)配置文件,將其它對象的引用通過組件提供的setter方法進行設(shè)定。運行期間動態(tài)生成對象并設(shè)定它對依賴關(guān)系.
Object obj = Class.forName("net.xiaxin.beans.User").newInstance();
BeanWrapper bw = new BeanWrapperImpl(obj);
bw.setPropertyValue("name", "Erica");
System.out.println("User name=>"+bw.getPropertyValue("name"));
Bean Factory
? 創(chuàng)建并且維護Bean實例它可以配置
??????1.? Bean的屬性值及其依賴關(guān)系(即對其它Bean的引用)
??????2.? Bean的創(chuàng)建模式(是否單例模式)
??????3.? Bean的初始化和銷毀方法
??????4.? Bean的依賴關(guān)系
ApplicationContext
??????覆蓋了BeanFactory所有的方法,而且提供了新的功能。它有以下擴展功能
???1.國際化的支持
???2.資源訪問
???3.事件傳播
???4.多實例加載
??? spring提供了可配置的ApplicationContext加載機制。加載器有兩種選擇:ContextLoaderListener或ContextLoaderServlet一個利用servlet2.3的Listener接口實現(xiàn)而另一個則利用Servlet接口實現(xiàn)。
在web.xml中加入
????????????<listener>
???????????????<listener-class>
??????????????????org.springframework.web.context.ContextLoaderListener
???????????? ?</listener-class>
?????????? </listener>
或者:
?????????<servlet>
????????????<servlet-name>context</servlet-name>
????????????<servlet-class>
??????????????????org.springframework.web.context.ContextLoaderServlet
????????????</servlet-class>
????????????<load-on-startup>1</load-on-startup>
?????????</servlet>
Web容器會自動加載WEB-INF/applicationContext.xml初始化ApplicationContext實例.如果你指定配置文件就可以通過context-param指定:
??????<context-param>
?????????<param-name>contextConfigLocation</param-name>
?????????<param-value>/WEB-INF/myApplicationContext.xml</param-value>
??????</context-param>
配置完成后就可以通過WebApplicationContextUtils.getWebApplicationContext方法在Web應(yīng)用中獲取ApplicationContext的引用.
spring至關(guān)重要的一環(huán)就是裝配,即配置文件的編寫,接下來我按剛才實際過程中一步步簡單講解。
首先,要在web.xml中配置DispatcherServlet,它是作為Spring MVC的前端控制器.必須在web.xml中配置好,如下
?
<
servlet
>?
?????<servlet-name>ntx</servlet-name>?
?????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>?
?????<load-on-startup>1</load-on-startup>?
</servlet>
實際上,spring的配置文件可以分切到多個xml文件,我們這個簡單的示例就把它配置到ntx.xml中
?
?<?xml?version="1.0"?encoding="UTF-8"?>?
<!DOCTYPE?beans?PUBLIC
????"-//SPRING//DTD?BEAN//EN"
????"http://www.springframework.org/dtd/spring-beans.dtd">?
?
<beans
??default-autowire="no"?
??default-lazy-init="false"?
??default-dependency-check="none"?
>?
?
????<bean?id="loginService"?class="ntx.service.serviceimpl.LoginServiceImpl"/>?
????????
????<bean??id="loginController"?class="ntx.controller.LoginController">?
????????<property?name="loginService">?
????????????<ref?bean="loginService"/>?
????????</property>?
????????<property?name="gotoUrl">?
????????????<value>/showResult.jsp</value>?
????????</property>?
????</bean>?
????
????<bean?id="SimpleUrlHandlerMapping"?class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">?
????????<property?name="mappings">?
????????????<props>?
????????????????<prop?key="/userLogin.do">loginController</prop>?
????????????</props>?
????????</property>?
????</bean>?
</beans>
?
配置好上面的這些后,要在WEB-INF下要建立ntx-servlet.xml如下:
?
?<?xml?version="1.0"?encoding="UTF-8"?>?
<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"?"http://www.springframework.org/dtd/spring-beans.dtd">?
<beans>?
????<bean?id="viewResolver"?class="org.springframework.web.servlet.view.InternalResourceViewResolver">?
????????<property?name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>?
????????<property?name="prefix"><value></value></property>?
????????<property?name="suffix"><value></value></property>?
????????
????</bean>?
</beans>
?
接下來,要指明哪些請求將交給spring的DispatcherServlet來處理,所以在web.xml中添加<servlet-mapping>
?<servlet-mapping>?
????????<servlet-name>ntx</servlet-name>?
????????<url-pattern>*.do</url-pattern>?
</servlet-mapping>
為了能正確載入DispatcherServlet等配置文件,我們要在web.xml中配置一個上下文載入器ContextLoaderListener或者ContextLoaderServlet,我們這里為了兼容版本較低的Serlvet容器(實際上我采用的2.4),采用第二種:
?
?<servlet>?
?????<servlet-name>context</servlet-name>?
?????<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>?
?????<load-on-startup>100</load-on-startup>?
</servlet>
這樣就全部配置完畢了,當然,上面的ntx.xml是我在項目完成以后才配置完成的,這里不再多講,有bean元素的配置大家可以參考有關(guān)資料理解,很容易理解的,下面再給出完整的web.xml配置以及java
<?xml?version="1.0"?encoding="UTF-8"?>
<web-app?version="2.4"?
????xmlns="http://java.sun.com/xml/ns/j2ee"?
????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?
????xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee?
????http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
???<context-param>
???????<param-name>contextConfigLocation</param-name>
???????<param-value>/WEB-INF/ntx.xml</param-value>
???</context-param>
???<servlet>
?????<servlet-name>ntx</servlet-name>
?????<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
?????<load-on-startup>1</load-on-startup>
???</servlet>
???<servlet>
?????<servlet-name>context</servlet-name>
?????<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
?????<load-on-startup>100</load-on-startup>
???</servlet>
???<servlet-mapping>
????????<servlet-name>ntx</servlet-name>
????????<url-pattern>*.do</url-pattern>
????</servlet-mapping>
????<welcome-file-list>
????????<welcome-file>index.jsp</welcome-file>
????</welcome-file-list>
</web-app>
根據(jù)ntx.xml知道,總共有三個java文件,LoginController.java是控制器,繼承了最簡單的Controller(實際上spring有很多控制器供我們選擇),接下來是一個簡單控制器的源碼
package?ntx.controller;
import?javax.servlet.http.HttpServletRequest;
import?javax.servlet.http.HttpServletResponse;
import?org.springframework.web.servlet.ModelAndView;
import?org.springframework.web.servlet.mvc.Controller;
import?ntx.service.LoginService;
public?class?LoginController?implements?Controller...{
????private?LoginService?loginService?;
????private?String?gotoUrl;
????public?ModelAndView?handleRequest(HttpServletRequest?request,HttpServletResponse?response)?throws?Exception?...{
????????String?userName?=?request.getParameter("userName");
????????this.getUserInfo(request,?userName);
????????
????????return?new?ModelAndView(this.getGotoUrl());
????}
????
????private?void?getUserInfo(HttpServletRequest?request,String?userName)...{
????????String?userInfo?=?loginService.getUserInfo(userName);
????????request.setAttribute("userInfo",?userInfo);
????}
????
????public?String?getGotoUrl()?...{
????????return?gotoUrl;
????}
????public?void?setGotoUrl(String?gotoUrl)?...{
????????this.gotoUrl?=?gotoUrl;
????}
????public?LoginService?getLoginService()?...{
????????return?loginService;
????}
????public?void?setLoginService(LoginService?loginService)?...{
????????this.loginService?=?loginService;
????}
????
}
還有service層的接口以及實現(xiàn),較簡單,
package?ntx.service;
public?interface?LoginService?...{
????public?String?getUserInfo(String?userName);
}
package?ntx.service.serviceimpl;
import?ntx.service.LoginService;
public?class?LoginServiceImpl?implements?LoginService?...{
????public?String?getUserInfo(String?userName)...{
????????
????????return?"你的名字是:"?+?userName;
????}
}
好了,最后是兩個jsp文件,一個index.jsp用來顯示一個表單,輸入名字,一個showResult.jsp用來顯示結(jié)果,只貼出相關(guān)的代碼
<body>
??? This is my Test Spring page. <br>
??? <div>
??? ?<form method="post" action="/userLogin.do">
???? ?<input type="text" name="userName" size="30"/><br/>
???? ?<input type="submit" value="提交"/>
???? </form>
??? </div>
? </body>
<body>
??? This is the Result: <br>
?<c:out value="${userInfo}" default="沒有結(jié)果"/>
? </body>
發(fā)布到tomcat或者其它Servlet容器可以正常使用,提交以后將顯示:
This is the Result:
你的名字是:gavin
?