spring3 mvc 入門配置

 

 

1. 下載    http://www.springsource.org/download/communityhtml

 

2. 建立web 工程兵導入jarjava

 

 

 

3.  配置web.xmlweb

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    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">
    <!-- 默認所對應的配置文件是WEB-INF下的{servlet-name}-servlet.xml,這裏即是:applicationContext-mvc.xml -->  
    <servlet>  
        <servlet-name>springmvc</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>springmvc</servlet-name>  
        <!-- 這裏能夠用 / 但不能用 /* ,攔截了全部請求會致使靜態資源沒法訪問,因此要在spring3-servlet.xml中配置mvc:resources -->  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

4. 建立spring mvc 配置文件spring

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
    default-lazy-init="true">

    <!-- 啓動註解驅動的Spring MVC功能,註冊請求url和註解POJO類方法的映射-->
    <mvc:annotation-driven />

    <!--
        啓動包掃描功能,以便註冊帶有@Controller、@Service、@repository、@Component等註解的類成爲spring的bean
    -->
    <context:component-scan base-package="com.ryan.controller" />

    <!-- 對模型視圖名稱的解析,在WEB-INF/jsp目錄下找對應的jsp文件 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
</beans>

 

5. 建立spring 配置文件  applicationContext.xmlspring-mvc

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"
    default-lazy-init="true">

</beans>

 

6. 建立Controllertomcat

package com.ryan.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
 * 控制器
 */
@Controller
public class HelloController {
    //URL映射
    @RequestMapping(value="/hello", method = RequestMethod.GET)
    public String hello() {
        return "/hello";
    }
}

 

7. 建立視圖文件,這裏爲jspmvc

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>My JSP 'hello.jsp' starting page</title>
  </head>
  <body>
    hello spring3mvc. <br>
  </body>
</html>

 

8. 啓動tomcat訪問app

工程目錄圖:jsp

 

參考:http://boytnt.blog.51cto.com/966121/1120792url

相關文章
相關標籤/搜索