{{{ #!html
Spring Web MVC Eclipse 編譯環境
Spring 3.1 + Eclipse 3.6EE + Tomcat 6 + WTP 3.2.4
}}} [[PageOutline]] = 前言 = * 改寫 http://caterpillar.onlyfun.net/Gossip/SpringGossip/FirstSpringMVC.html,但按照此網頁程式跑不出來,因此而外補充之。 補充 * [http://blog.springsource.com/2011/01/04/green-beans-getting-started-with-spring-mvc/?__utma=1.418358013.1314090089.1314345749.1315450496.5&__utmb=1.1.10.1315450496&__utmc=1&__utmx=-&__utmz=1.1315450496.5.5.utmcsr=stackoverflow.com|utmccn=%28referral%29|utmcmd=referral|utmcct=/questions/2260552/getting-started-with-spring-3-web-mvc-setting-everything-up&__utmv=-&__utmk=197304879 官方doc ] * [http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/ 有非常多有用的範例含程式碼] = Eclipse = * 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 == 新增專案 == 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)]]