学习springMVC-最少jar包及简单搭建

    • jar包下载
    • 新建web项目保留webxml
    • 配置webxml文件WEB-INF下
    • 配置springmvcxml文件在src下 classpath即为src
    • 导入jar包
    • 编写控制器
    • 编写welcomejsp页面在WEB-INFjsp下

1.jar包下载

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

2.新建web项目,保留web.xml.

3.配置web.xml文件(/WEB-INF/下):


<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>SpringMVCservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>SpringMVCservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>
web-app>

4.配置springmvc.xml文件(在/src下 classpath即为/src)


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

        
        <context:component-scan base-package="com.test.springmvc">context:component-scan>


        
        <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包及简单搭建_第1张图片

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

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
欢迎!
body>
html>

你可能感兴趣的:(SpringMVC)