1. 程式人生 > >學習springMVC-最少jar包及簡單搭建

學習springMVC-最少jar包及簡單搭建

1.jar包下載

http://repo.spring.io/release/org/springframework/spring/4.2.4.RELEASE/ 

2.新建web專案,保留web.xml.

3.配置web.xml檔案(/WEB-INF/下):

<?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" 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">

  <servlet
>
<servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置SpringMVC下的配置檔案位置及名稱 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value
>
classpath:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

4.配置springmvc.xml檔案(在/src下 classpath即為/src)

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">

        <!-- 自動掃描controller包下的所有類,使其認為是spring mvc的控制器 路徑即為類路徑 -->
        <context:component-scan base-package="com.test.springmvc"></context:component-scan>


        <!-- 配置檢視解析器 如何把handler 方法返回值解析為實際的物理檢視  根據控制器返回的字串拼接成jsp路徑:/WEB-INF/jsp/xx.jsp -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name = "prefix" value="/WEB-INF/jsp/"></property>
            <property name = "suffix" value = ".jsp"></property>
        </bean>
</beans>

5.匯入jar包

(1) 直接啟動tomcat:
警告: No mapping found for HTTP request with URI [/SpringTest/] in DispatcherServlet with name ‘SpringMVC’
因為在web.xml中引入了org.springframework.web.servlet.DispatcherServlet,所以需要匯入spring-mvc包
(2)再來:
java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextAware
匯入spring-context包
(3)再啟動:
java.lang.ClassNotFoundException: org.springframework.beans.factory.Aware
匯入spring-bean包
(4)再來:
java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable
匯入spring-core包
(5)繼續:
Caused by: java.lang.NoClassDefFoundError: org/springframework/web/context/request/async/CallableProcessingInterceptor
匯入spring-web包
(6)再來:
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;
匯入commons-logging包
(7)繼續:
Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
匯入spring-aop包
(8)接著:
java.lang.NoClassDefFoundError: org/springframework/expression/ParserContext
匯入spring-expression包,再啟動就不報錯了。
所以最小jar包是
SpringMVC最小jar包

6.編寫控制器:

package com.test.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Show {
    @RequestMapping("/welcome")
    public String welcome() {
        return "welcome";
    }
}

7.編寫welcome.jsp頁面(在/WEB-INF/jsp/下)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
歡迎!
</body>
</html>