使用jsp作为视图(其实推荐是使用thymeleaf作为视图的,但是辛苦配置了那么久,还是记录一下吧。。。)
完全基于Java 的配置
工程目录如图
1. 首先用 mvn 创建一个 webapp 工程,使用pom.xml配置项目和依赖
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ly</groupId>
<artifactId>example</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>example Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
<finalName>example</finalName>
</build>
</project>
包括springmvc必要的依赖,javax.servlet-api,jstl(一开始不知道要后两个,弄了很久。。)
最后添加 tomcat7 的集成插件
2. 定义一个 controller
package com.ly.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by Shower on 2017/4/1 0001.
*/
@Controller
public class HelloController {
@RequestMapping(value = "/")
public String sayHello(ModelMap modelMap) {
modelMap.put("msg", "Hello!");
return "hello";
}
}
使用@Controller注解进行配置,@RequestMapping进行地址映射,返回 jsp 模板的名称3. 在WEB-INF文件夹下创建views文件夹,并在其中创建hello.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h2>Message: ${msg}</h2>
</body>
</html>
4. SpringMVC配置
package com.ly.web;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
/**
* Created by Shower on 2017/4/1 0001.
*/
@Configuration
@ComponentScan
@EnableWebMvc
public class WebConfig {
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new
InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
@Configuration和@Bean一起用来配置Bean,@ComponentScan用来扫描当前包下用注解配置的Bean,@EnableWebMVC启用注解配置SpringMVC
5. 配置DispatcherServlet
package com.ly;
import com.ly.business.BusinessConfig;
import com.ly.web.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Created by Shower on 2017/4/1 0001.
*/
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] {BusinessConfig.class};
}
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] {WebConfig.class};
}
protected String[] getServletMappings() {
return new String[] {"/"};
}
}
package com.ly.business;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Created by Shower on 2017/4/1 0001.
*/
@Configuration
@ComponentScan
@EnableWebMvc
public class BusinessConfig {
}
6. 输入命令 mvn tomcat7:run 运行
访问 http:localhost:8080/example/