Spring-MVC-所需要的jar包

这两天因为环境搭建的问题特别苦难,参考了一下网上的各种答案,决定自己试一下Spring-MVC启动所需要的jar包;

因为Spring-MVC是在Spring框架下,索性从零开始我们引入需要的jar包;

maven下创建工程老师还没有讲解,这里采用eclipse的Dynamic Web Project来搭建环境;

首先配置web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 4     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5     id="WebApp_ID" version="3.1">
 6     <servlet>
 7         <servlet-name>springMVC</servlet-name>
 8         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 9         <init-param>
10             <param-name>contextConfigLocation</param-name>
11             <param-value>/WEB-INF/servlet-context.xml</param-value>
12         </init-param>
13         <load-on-startup>1</load-on-startup>
14     </servlet>
15     <servlet-mapping>
16         <servlet-name>springMVC</servlet-name>
17         <url-pattern>/</url-pattern>
18     </servlet-mapping>
19 
20     <display-name>Test</display-name>
21     <welcome-file-list>
22         <welcome-file>index.html</welcome-file>
23         <welcome-file>index.htm</welcome-file>
24         <welcome-file>index.jsp</welcome-file>
25         <welcome-file>default.html</welcome-file>
26         <welcome-file>default.htm</welcome-file>
27         <welcome-file>default.jsp</welcome-file>
28     </welcome-file-list>
29 </web-app>
View Code

 然后配置spring-MVC的xml→servlet-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    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.xsd">

  <annotation-driven />

  <context:component-scan base-package="spitter" />

  <beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
  </beans:bean>

  <resources mapping="/resources/**" location="/resources/" />

  <!-- 
  <view-controller path="/" view-name="home" />
  -->

</beans:beans>
View Code

启动项目

tomcat8.5启动报错

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

把spring-web-4.2.3.RELEASE.jar 拖到/WEB-INF/lib/目录下,Refresh一下工程;

再次启动项目后报错

java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

把spring-webmvc-4.2.3.RELEASE.jar 拖到/WEB-INF/lib/目录下,Refresh一下工程;

再次启动项目后报错

java.lang.ClassNotFoundException: org.springframework.context.ApplicationContextAware

把spring-context-4.2.3.RELEASE.jar 拖到/WEB-INF/lib/目录下,Refresh一下工程;

再次启动项目后报错

java.lang.ClassNotFoundException: org.springframework.beans.factory.Aware

把spring-beans-4.2.3.RELEASE.jar 拖到/WEB-INF/lib/目录下,Refresh一下工程;

再次启动项目后报错

java.lang.ClassNotFoundException: org.springframework.core.env.EnvironmentCapable

把spring-core-4.2.3.RELEASE.jar 拖到/WEB-INF/lib/目录下,Refresh一下工程;

再次启动项目后报错

java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException:

 等等等这个问题没解决,哈哈 ,然后睡了一大觉把所有的jar包从bin中拿掉,做了个零配置java config的启动方式,竟然成功了

基本环境:tomcat8.5、jdk1.8、eclipse Dynamic Web Project工程

基本就4个类就能实现mvc环境了

首先是SpitterWebInitializer这个类继承了AbstractAnnotationConfigDispatcherServletInitializer,

而tomcat容器启动时能够找到AbstractAnnotationConfigDispatcherServletInitializer;

package spittr.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import spittr.web.WebConfig;

public class SpitterWebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
  @Override
  protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
  }

  @Override
  protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
  }

  @Override
  protected String[] getServletMappings() {
    return new String[] { "/" };
  }

}
View Code

AbstractAnnotationConfigDispatcherServletInitializer找到WebConfig.java,WebConfig.java中配置了视图解析器,另外两个暂时不知道功能;

package spittr.web;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("spittr.web")
public class WebConfig extends WebMvcConfigurerAdapter {

    //配置视图解析器
  @Bean
  public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
  }
  
  @Override
  public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
  }
  
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    // TODO Auto-generated method stub
    super.addResourceHandlers(registry);
  }

}
View Code

有了视图解析器我们需要一个controller,那就是HomeController.java,返回一个视图解析器需要的home

package spittr.web;

import static org.springframework.web.bind.annotation.RequestMethod.*;

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

@Controller
@RequestMapping("/")
public class HomeController {

  @RequestMapping(method = GET)
  public String home(Model model) {
    return "home";
  }

}
View Code

最后我们只需要在/WEB-INF/views/路径下创建一个home.java

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
  <head>
    <title>Spitter</title>
    <link rel="stylesheet" 
          type="text/css" 
          href="<c:url value="/resources/style.css" />" >
  </head>
  <body>
    <h1>Welcome to Spitter</h1>

    <a href="<c:url value="/spittles" />">Spittles</a> | 
    <a href="<c:url value="/spitter/register" />">Register</a>
  </body>
</html>
View Code

到此结束;

package spittr.config;

import java.util.regex.Pattern;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.core.type.filter.RegexPatternTypeFilter;

import spittr.config.RootConfig.WebPackage;

@Configuration
@ComponentScan(basePackages={"spittr"}, 
    excludeFilters={
        @Filter(type=FilterType.CUSTOM, value=WebPackage.class)
    })
public class RootConfig {
  public static class WebPackage extends RegexPatternTypeFilter {
    public WebPackage() {
      super(Pattern.compile("spittr\\.web"));
    }    
  }
}

这个rootConfig具体作用我稍后补充

 

posted on 2017-08-31 11:55  剑姬  阅读(1895)  评论(0编辑  收藏  举报

导航