{{{ #!html
Spring Web MVC Eclipse 編譯環境
Spring 3.1 + Eclipse 3.6EE + Tomcat 6 + WTP 3.2.4
}}} [[PageOutline]] = 安裝環境 = * Spring 3.1 * Eclipse 3.6EE * Tomcat 6 下載 tomcat 專案到系統內 * WTP 3.2.4 在 Eclipse->Help->"install new software" 安裝 WTP 3.2.4 與 WTP 3.2.4 SDK = Eclipse = * 改寫 http://caterpillar.onlyfun.net/Gossip/SpringGossip/FirstSpringMVC.html,但按照此網頁程式跑不出來,因此而外補充之。 == 新增專案 == new project -> other -> "web" : "Dynamic Web Project" -> project name = "Web1SpringMVC" -> 完成 == 匯入Web App Libraties == 加入所有spring*.jar 到 WebContent/WEB-INF/lib/ 內 用 指令 或用 file->import->"file system" 精靈 將 spring/dist/*.jar 匯入到此資料夾 == 匯入Java Resources == 在 java build path -> libraries : add external jars -> 選擇 spring/dist/*.jar -> ok == 程式碼 == * WebContent/WEB-INF/jsp/hello.jsp {{{ #!java <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> First Spring MVC

Hello, ${user}!!

usage: hello.do?user=xx

}}} * WebContent/WEB-INF/web.xml {{{ #!xml 30 DispatcherServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation /WEB-INF/mvc-config.xml 1 DispatcherServlet *.do }}} * WebContent/WEB-INF/mvc-config.xml {{{ #!xml /WEB-INF/jsp/ .jsp hello }}} * "Java Resource"-> src/org.waue/HelloController.java {{{ #!java package org.waue; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller { private String viewPage; public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse res) throws Exception { String user = req.getParameter("user"); return new ModelAndView(viewPage, "user", user); } public void setViewPage(String viewPage) { this.viewPage = viewPage; } } }}} [[Image(1.png,800)]] = 結果 = http://localhost:8080/Web1SpringMVC/hello.do?user=aa [[Image(2.png)]]