wiki:waue/2011/0907
Spring Web MVC Eclipse 編譯環境
Spring 3.1 + Eclipse 3.6EE + Tomcat 6 + WTP 3.2.4

前言

補充

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
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<!doctype html>
<html>
    <head>
      <meta charset="UTF-8">
        <title>First Spring MVC</title>
    </head>
    <body>
        <h1>Hello, ${user}!!</h1>
        <p>usage: hello.do?user=xx</p>
    </body>
    
</html>

  • WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee →" version="2.4">
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>
  <servlet>
    <servlet-name>DispatcherServlet</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <init-param>
      <javaee:param-name>contextConfigLocation</javaee:param-name>
      <javaee:param-value>/WEB-INF/mvc-config.xml</javaee:param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

  • WebContent/WEB-INF/mvc-config.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="prefix">
            <value>/WEB-INF/jsp/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>
    
    <bean name="/hello.do" 
          class="org.waue.HelloController"> 
        <property name="viewPage"> 
            <value>hello</value> 
        </property> 
    </bean>
</beans>
  • "Java Resource"-> src/org.waue/HelloController.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;
    }
}

結果

http://localhost:8080/Web1SpringMVC/hello.do?user=aa

Last modified 9 years ago Last modified on Sep 8, 2011, 11:34:18 AM

Attachments (2)

Download all attachments as: .zip