原帖地址:http://blog.csdn.net/wzl002/article/details/5969469
這次從零開始建一個web項目, 發現網上針對spring 3.0的講解不多, 所以藉此機會記錄一下配置一個基本的web項目, 主要是配置spring3的過程, 其實大部分和2是類似的.
第一步: 建立並部署一個web項目, (可跳過)
1. 這裡想關於web項目簡單說一句, 其實一個wen工程最基本的,只看3個地方:
在根目錄下(這個目錄一般習慣叫WebContext或WebRoot)有
1. WebRoot/WEB-INF/web.xml 啟動引導檔案
2.WebRoot/WEB-INF/classes/ 編譯的class檔案,會根據package建立子路徑
3.WebRoot/WEB-INF/lib/ jar包 (注意,lib下不可以再建子目錄)
這就是一般比較常用的web工程的結構. 有了這三個結構, 然後把WebRoot的絕對路徑告訴tomcat這樣的web容器,就可以啟動了(當然裡面要有相應的東西才行).
因此雖然我們一般項目的結構是: projectname/src, projectname/WebRoot 但實際上src裡的.java原始碼是tomcat完全不關心的,它的解析是從WebRoot開始的, .class才是它認識的.
下面是一個初始的web.xml. 這個檔案就相當於項目的啟動引導檔案, web容器最先找到這個檔案,然後解讀並一一啟動這些類.
view plain
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app 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"
- version="2.5">
-
-
- <servlet>
- <servlet-name>TestServlet</servlet-name>
- <display-name>測試用Servlet</display-name>
- <servlet-class>test.TestServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>TestServlet</servlet-name>
- <url-pattern>/test/*</url-pattern>
- </servlet-mapping>
-
-
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
2.部署至tomcat:
tomcat的部署方式有很多, 我比較喜歡的部署方式是apache-tomcat/conf/Catalina/localhost/ 下放入projectname.xml設定檔,
內容為
view plain
- <Context path="projectname" reloadable="true" docBase="D:/workspace/projectname/WebContent" ></Context>
工程就部署完了. 簡單吧.
然後在eclipse裡也不用去配什麼server,運行環境. 裝一個tomcat外掛程式,指定tomcat路徑就行了. 上面的xml檔案也可以通過配置eclipse-tomcat外掛程式自動產生.
第二步: 把spring的jar包引進來, 這可不是廢話哦. 見過spring3.0的都知道, spring3把之前的一個萬能spring.jar分拆成了20個小jar. 當然你也可以省事的直接把20個都引進來,沒問題.
不過如果你不想這麼幹, 這裡是一個簡單能在web中實現最基本的依賴注入所需的jar包:
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
commons-logging-1.1.1.jar
其中commons-logging-1.1.1.jar 是spring的依賴包.
以上是一個不含spring-jdbc資料庫管理的合集, 如果要用spring的持久層,還需要:
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
commons-dbcp-1.3.jar
commons.pool-1.5.3.jar
同樣,後面2個也是spring的依賴包.
關於spring持久層的配置在下一節介紹
第三步: 配置spring, 基本和spring2還是一樣的.
1 在web.xml中加入
view plain
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>/WEB-INF/spring*.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
以前見通過配置spring的Servlet方式, 那種是為了對應在一些低版本的web容器. 這裡就不講了.
2. 在WEB-INF/ 下建一個 spring-conf.xml (名字和路徑都隨便了, 跟第一步的配置對應就行了.不過出於安全考慮還是不要放到WEB-INF外面.)
view plain
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
-
- <bean id="TestBo"
- class="test.TestBo">
- </bean>
-
- <bean id="TestAction" class="test.TestAction">
- <property name="TestBo" ref="TestBo" />
- </bean>
- </beans>
這裡spring的配置就算完成了, 然後把測試類別實現一下, 就可以測測效果啦. 基本除了jar包以外,和spring2都是一致的.
下面是TestServlet , 其他的TestAction和TestBo就隨便寫了. 啟動後訪問
http://127.0.0.1:8080/projectname/test/abc 就可以看到執行輸出了
view plain
- package test;
-
- import java.io.IOException;
-
- import javax.servlet.ServletConfig;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
-
- public class TestServlet extends HttpServlet {
-
- TestAction ta ;
- public TestServlet()
- {
- }
-
- public void init(ServletConfig servletConfig)
- throws ServletException{
- servletConfig.getServletContext();
- ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletConfig.getServletContext());
- this.ta = ctx.getBean(TestAction.class);
- this.ta.sayHello();
- System.out.println("TestServlet init");
- // TestAction ta = new TestAction();
- // ta.sayHello();
- }
-
-
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws IOException, ServletException
- {
- doPost(req, resp);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws IOException, ServletException
- {
- this.ta.sayHello();
- }
-
- public void destroy()
- {
- }
- }
.