1. 程式人生 > >Spring MVC + TomCat 環境搭建

Spring MVC + TomCat 環境搭建

1.spring 需要的jar 包

spring框架配置

<?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/javaee"   
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    id="WebApp_ID"   
    version="3.0">  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <!-- 應用上下文配置檔案 -->  
        <param-value>/WEB-INF/spring-servlet.xml</param-value>  
    </context-param>  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!-- 配置spring核心servlet -->  
    <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <!-- url-pattern配置為/,不帶檔案字尾,會造成其它靜態檔案(js,css等)不能訪問。如配為*.do,則不影響靜態檔案的訪問 -->  
    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>  
</web-app> 

2、應用上下文配置
spring-servlet.xml即配置用於開啟基於註解的springMVC功能,照web.xml中設定,路徑為WEB-INF下

    <beans xmlns="http://www.springframework.org/schema/beans"  
     xmlns:context="http://www.springframework.org/schema/context"  
     xmlns:p="http://www.springframework.org/schema/p"  
     xmlns:mvc="http://www.springframework.org/schema/mvc"  
     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  
          http://www.springframework.org/schema/context  
          http://www.springframework.org/schema/context/spring-context.xsd  
          http://www.springframework.org/schema/mvc  
          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">  
         <!-- 啟動註解驅動的Spring MVC功能,註冊請求url和註解POJO類方法的對映-->  
         <mvc:annotation-driven />  
         <!-- 啟動包掃描功能,以便註冊帶有@Controller、@Service、@repository、@Component等註解的類成為spring的bean -->  
         <context:component-scan base-package="com.mvc.rest" />  
         <!-- 對模型檢視名稱的解析,在請求時模型檢視名稱新增前後綴 -->  
         <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp" />  
    </beans>  
 

Demo例子
1、根據spring-servlet.xml配置的包路徑(com.mvc.rest)新建Constroller

package com.mvc.rest;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import org.springframework.stereotype.Controller;  
import org.springframework.ui.ModelMap;  
import org.springframework.web.bind.annotation.PathVariable;  
import org.springframework.web.bind.annotation.RequestMapping;  
import org.springframework.web.bind.annotation.RequestMethod;  
import org.springframework.web.servlet.ModelAndView;  
@Controller  
public class RestConstroller {  
    public RestConstroller() {}  
    @RequestMapping(value = "/login/{user}", method = RequestMethod.GET)  
    public ModelAndView myMethod(HttpServletRequest request,HttpServletResponse response,   
            @PathVariable("user") String user, ModelMap modelMap) throws Exception {  
        modelMap.put("loginUser", user);  
        return new ModelAndView("/login/hello", modelMap);  
    }  
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)  
    public String registPost() {  
        return "/welcome";  
    }  
}  


2、建jsp檢視

檢視路徑在spring-servlet.xml配置(/WEB-INF/view/),據上述RestConstroller 類,我們在WEB-INF下建立view目錄,在view下建立welcome.jsp及login/hello.jsp
welcome.jsp隨意,hello.jsp程式碼如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
    <%  
    String path = request.getContextPath();  
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
    %>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
    <html>  
    <head>  
    <base href="<%=basePath%>">  
    <title>My JSP 'hello.jsp' starting page</title>  
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    </head>  
    <body>  
        你好:<%=request.getAttribute("loginUser") %>,現在時間是<%= new Date() %>  
    </body>  
    </html>