不用web.xml,而使用java类配置SpringMVC

不用web.xml,而使用java类配置SpringMVC

DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servlet 3规范和Spring 3.1的功能增强, 可以采用一种全新的,更简洁的方式配置Spring MVC了. 下面按这种方式一个Hello World的MVC配置.

Step 1:先用idea创建一个Maven的WEB工程. pom.xml文件如下:

不用web.xml,而使用java类配置SpringMVC_第1张图片

OcrWebAppInitializer:

package com.home.config;

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


public class OcrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {



 @Override

 protected Class[] getRootConfigClasses() {

 return new Class[] { RootConfig.class };

 }



 @Override

 protected Class[] getServletConfigClasses() {

 return new Class[] { WebConfig.class };        //ָ指定Web配置类

 }



 @Override

 protected String[] getServletMappings() {    //将DispatcherServlet映射到"/"

 return new String[] { "/" };

 }



}

RootConfig

package com.home.config;

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.web.servlet.config.annotation.EnableWebMvc;



@Configuration

@ComponentScan( basePackages={"com.home"},

 excludeFilters = { @Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)}

)



public class RootConfig {



}

WebConfig

package com.home.config;



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.WebMvcConfigurerAdapter;

import org.springframework.web.servlet.view.InternalResourceViewResolver;


@Configuration

@EnableWebMvc                                //启动SpringMVC

@ComponentScan("com.home.controller")            //启动组件扫描

public class WebConfig extends WebMvcConfigurerAdapter {



 //配置JSP视图解析器

 @Bean

 public ViewResolver viewResolver() {

 InternalResourceViewResolver resolver = new InternalResourceViewResolver();

 resolver.setPrefix("/WEB-INF/views/");

 resolver.setSuffix(".jsp");

 resolver.setExposeContextBeansAsAttributes(true);

 return resolver;

 }



 //配置静态资源的处理

 @Override

 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

 configurer.enable();        //对静态资源的请求转发到容器缺省的servlet,而不使用DispatcherServlet

 }


}

HomeController

package com.home.controller;

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;

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

import org.springframework.web.servlet.ModelAndView;



@Controller

public class HomeController {

 @RequestMapping(value = "/home", method=GET)

 public String home() {

 return "home";

 }

}

home.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="UTF-8"%>



<html>

<head>

 <title>Spring MVC Tutorial chrytitle>

 <style type="text/css">

 style>

head>

<body>

<br>

<div style='text-align:center;'>

 index page!

div>

body>pre>

pom.xml:

"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 4.0.0



 com

 springMVC-config

 1.0-SNAPSHOT

 war



 springMVC-config Maven Webapp

 

 http://www.example.com



 

 UTF-8

 1.7

 1.7

 



 

 

 junit

 junit

 4.11

 test

 



 

 junit

 junit

 4.11

 test

 





 

 org.slf4j

 slf4j-api

 1.7.12

 

 

 ch.qos.logback

 logback-core

 1.1.1

 provided

 

 

 

 ch.qos.logback

 logback-classic

 1.1.1

 provided

 



 

 

 com.alibaba

 druid

 1.0.25

 

 !--2.dao框架:MyBatis依赖--

 

 org.mybatis

 mybatis

 3.3.0

 

 

 

 org.mybatis

 mybatis-spring

 1.3.2

 



 

 

 taglibs

 standard

 1.1.2

 

 

 jstl

 jstl

 1.2



 

 

 com.fasterxml.jackson.core

 jackson-databind

 2.5.4

 



 

 javax.servlet

 javax.servlet-api

 3.1.0

 provided

 



 !--4:spring依赖--

 

 

 org.springframework

 spring-core

 4.3.14.RELEASE

 

 

 org.springframework

 spring-beans

 4.3.14.RELEASE

 

 

 org.springframework

 spring-context

 4.3.14.RELEASE

 

 

 

 org.springframework

 spring-jdbc

 4.3.14.RELEASE

 

 

 org.springframework

 spring-tx

 4.3.14.RELEASE

 

 

 

 org.springframework

 spring-web

 4.3.14.RELEASE

 

 

 org.springframework

 spring-webmvc

 4.3.14.RELEASE

 

 

 

 org.springframework

 spring-test

 4.3.14.RELEASE

 

 

 mysql

 mysql-connector-java

 5.1.35

 runtime

 



 

 

 org.aspectj

 aspectjweaver

 1.8.9

 

 

 

 org.json

 json

 20170516

 





 

 

 org.thymeleaf

 thymeleaf-spring4

 3.0.9.RELEASE

 

 

 javax.validation

 validation-api

 2.0.1.Final

 

 

 com.google.guava

 guava

 18.0

 

 

 com.google.guava

 guava

 18.0

 

 

 org.hibernate

 hibernate-validator

 5.3.6.Final

 





 

 

 javax.servlet

 javax.servlet-api

 3.0.1

 

 

 javax.servlet.jsp

 jsp-api

 2.1

 provided

 

 

 javax.servlet

 jstl

 1.2

 

 

 



 

 springMVC-config

 

 

 

 maven-clean-plugin

 3.0.0

 

 

 

 maven-resources-plugin

 3.0.2

 

 

 maven-compiler-plugin

 3.7.0

 

 

 maven-surefire-plugin

 2.20.1

 

 

 maven-war-plugin

 3.2.0

 

 

 maven-install-plugin

 2.5.2

 

 

 maven-deploy-plugin

 2.8.2

 

 

 

 


这个时候访问相应的路径就可以看到jsp的相关信息了

你可能感兴趣的:(Spring)