天天看點

不用web.xml,而使用java類配置SpringMVC不用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不用web.xml,而使用java類配置SpringMVC

image

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

 <title>Spring MVC Tutorial chry</title>

 <style type="text/css">

 </style>

</head>

<body>

<br>

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

 index page!

</div>

</body></pre>

           

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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">

 <modelVersion>4.0.0</modelVersion>



 <groupId>com</groupId>

 <artifactId>springMVC-config</artifactId>

 <version>1.0-SNAPSHOT</version>

 <packaging>war</packaging>



 <name>springMVC-config Maven Webapp</name>

 <!-- FIXME change it to the project's website -->

 <url>http://www.example.com</url>



 <properties>

 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

 <maven.compiler.source>1.7</maven.compiler.source>

 <maven.compiler.target>1.7</maven.compiler.target>

 </properties>



 <dependencies>

 <dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.11</version>

 <scope>test</scope>

 </dependency>



 <dependency>

 <groupId>junit</groupId>

 <artifactId>junit</artifactId>

 <version>4.11</version>

 <scope>test</scope>

 </dependency>





 <dependency>

 <groupId>org.slf4j</groupId>

 <artifactId>slf4j-api</artifactId>

 <version>1.7.12</version>

 </dependency>

 <dependency>

 <groupId>ch.qos.logback</groupId>

 <artifactId>logback-core</artifactId>

 <version>1.1.1</version>

 <scope>provided</scope>

 </dependency>

 <!--實作slf4j接口并整合-->

 <dependency>

 <groupId>ch.qos.logback</groupId>

 <artifactId>logback-classic</artifactId>

 <version>1.1.1</version>

 <scope>provided</scope>

 </dependency>



 <!--druid==>阿裡巴巴資料庫連接配接池-->

 <dependency>

 <groupId>com.alibaba</groupId>

 <artifactId>druid</artifactId>

 <version>1.0.25</version>

 </dependency>

 !--2.dao架構:MyBatis依賴--

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis</artifactId>

 <version>3.3.0</version>

 </dependency>

 <!--mybatis自身實作的spring整合依賴-->

 <dependency>

 <groupId>org.mybatis</groupId>

 <artifactId>mybatis-spring</artifactId>

 <version>1.3.2</version>

 </dependency>



 <!--3.Servlet web相關依賴-->

 <dependency>

 <groupId>taglibs</groupId>

 <artifactId>standard</artifactId>

 <version>1.1.2</version>

 </dependency>

 <dependency>

 <groupId>jstl</groupId>

 <artifactId>jstl</artifactId>

 <version>1.2</version>



 </dependency>

 <dependency>

 <groupId>com.fasterxml.jackson.core</groupId>

 <artifactId>jackson-databind</artifactId>

 <version>2.5.4</version>

 </dependency>



 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>javax.servlet-api</artifactId>

 <version>3.1.0</version>

 <scope>provided</scope>

 </dependency>



 !--4:spring依賴--

 <!--1)spring核心依賴-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-core</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-beans</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-context</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--2)spring dao層依賴-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-jdbc</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-tx</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--3)springweb相關依賴-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-web</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-webmvc</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <!--4)spring test相關依賴-->

 <dependency>

 <groupId>org.springframework</groupId>

 <artifactId>spring-test</artifactId>

 <version>4.3.14.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>mysql</groupId>

 <artifactId>mysql-connector-java</artifactId>

 <version>5.1.35</version>

 <scope>runtime</scope>

 </dependency>



 <!--Aop要導入的包-->

 <dependency>

 <groupId>org.aspectj</groupId>

 <artifactId>aspectjweaver</artifactId>

 <version>1.8.9</version>

 </dependency>

 <!-- https://mvnrepository.com/artifact/org.json/json -->

 <dependency>

 <groupId>org.json</groupId>

 <artifactId>json</artifactId>

 <version>20170516</version>

 </dependency>





 <!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->

 <dependency>

 <groupId>org.thymeleaf</groupId>

 <artifactId>thymeleaf-spring4</artifactId>

 <version>3.0.9.RELEASE</version>

 </dependency>

 <dependency>

 <groupId>javax.validation</groupId>

 <artifactId>validation-api</artifactId>

 <version>2.0.1.Final</version>

 </dependency>

 <dependency>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 <version>18.0</version>

 </dependency>

 <dependency>

 <groupId>com.google.guava</groupId>

 <artifactId>guava</artifactId>

 <version>18.0</version>

 </dependency>

 <dependency>

 <groupId>org.hibernate</groupId>

 <artifactId>hibernate-validator</artifactId>

 <version>5.3.6.Final</version>

 </dependency>





 <!-- jsp -->

 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>javax.servlet-api</artifactId>

 <version>3.0.1</version>

 </dependency>

 <dependency>

 <groupId>javax.servlet.jsp</groupId>

 <artifactId>jsp-api</artifactId>

 <version>2.1</version>

 <scope>provided</scope>

 </dependency>

 <dependency>

 <groupId>javax.servlet</groupId>

 <artifactId>jstl</artifactId>

 <version>1.2</version>

 </dependency>

 <!-- jsp -->

 </dependencies>



 <build>

 <finalName>springMVC-config</finalName>

 <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->

 <plugins>

 <plugin>

 <artifactId>maven-clean-plugin</artifactId>

 <version>3.0.0</version>

 </plugin>

 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->

 <plugin>

 <artifactId>maven-resources-plugin</artifactId>

 <version>3.0.2</version>

 </plugin>

 <plugin>

 <artifactId>maven-compiler-plugin</artifactId>

 <version>3.7.0</version>

 </plugin>

 <plugin>

 <artifactId>maven-surefire-plugin</artifactId>

 <version>2.20.1</version>

 </plugin>

 <plugin>

 <artifactId>maven-war-plugin</artifactId>

 <version>3.2.0</version>

 </plugin>

 <plugin>

 <artifactId>maven-install-plugin</artifactId>

 <version>2.5.2</version>

 </plugin>

 <plugin>

 <artifactId>maven-deploy-plugin</artifactId>

 <version>2.8.2</version>

 </plugin>

 </plugins>

 </pluginManagement>

 </build>

</project>

           

這個時候通路相應的路徑就可以看到jsp的相關資訊了