开发环境:IDEA2017.1.6+Spring4.1.4+Tomcat7.0.57【有问题的匹配】
IDEA2017.1.6+Spring4.1.4+Tomcat8.0.43 【正确的匹配】
通过实际的案例演示:
项目的最终目录结构如下:
下面先展示不同版本匹配后的效果,然后罗列代码,最终做出总结。
1. IDEA2017.1.6+Spring4.1.4+Tomcat7.0.57
加载工程启动Tomcat7,运行演示项目后,最终页面展示效果如下图:
2. IDEA2017.1.6+Spring4.1.4+Tomcat8.0.43
加载工程启动Tomcat7,运行演示项目后,最终页面展示效果如下图:
通过以上的页面显示对比,可以看出JSTL、EL表达式功能失效了。
下面给出本案例的代码供参考:
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.web</groupId>
<artifactId>springmvcdemo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvcdemo 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>
<org.springframework-version>4.1.4.RELEASE</org.springframework-version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- Spring MVC support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<!-- Tag libs support for view layer -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<finalName>springmvcdemo</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>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Archetype Created Web Application</display-name>
<!-- 字符集 过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
spring-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启spring的扫描注入,使用如下注解 -->
<!-- @Component,@Repository,@Service,@Controller-->
<context:component-scan base-package="com.dk"/>
<!-- 开启springMVC的注解驱动,使得url可以映射到对应的controller -->
<mvc:annotation-driven />
<!-- 视图解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
studentDisplay.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Spring MVC Hello World</title>
</head>
<body>
<h2>All Students in System</h2>
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Age</th>
<th>Sex</th>
</tr>
<c:forEach var="student" items="${students}">
<tr>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
<td>${student.sex}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
StudentController.java
package com.dk.controller;
import com.dk.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
/**
* Created by dk on 2017/3/27
*/
@Controller
@RequestMapping("student")
public class StudentController {
@Resource(name = "studentService")
private StudentService studentService;
//@ResponseBody(之前我因为加了这个注解,导致页面访问一直是406错误,注释了就好啦,具体为啥我暂时还不知道)
@RequestMapping(value = "/getAllStudent", method = RequestMethod.GET)
public ModelAndView getAllStudent() {
ModelAndView mav = new ModelAndView();
mav.setViewName("studentDisplay");
mav.addObject("students", studentService.getAllStudent());
return mav;
}
}
StudentService.java
package com.dk.service;
import com.dk.dao.StudentDao;
import com.dk.model.Student;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* Created by dk on 2017/3/27
*/
@Service("studentService")
public class StudentService {
@Resource(name = "studentDao")
private StudentDao dao;
public List<Student> getAllStudent() {
return dao.findAll();
}
}
StudentDao.java
package com.dk.dao;
import com.dk.model.Student;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
/**
* Created by dk on 2017/3/27
*/
@Repository("studentDao")
public class StudentDao {
public List<Student> findAll() {
List<Student> all = new ArrayList<Student>();
all.add(new Student("1", "tom", 18, true));
all.add(new Student("2", "cat", 28, true));
all.add(new Student("3", "lxk", 18, true));
all.add(new Student("4", "cms", 18, true));
return all;
}
public List<Student> findAll_DB() {
return null;
}
}
Student.java
package com.dk.model;
import java.io.Serializable;
/**
* Created by dk on 2017/3/26
*/
public class Student implements Serializable {
private String id;
private String name;
private int age;
private boolean sex;
public Student() {
}
public Student(String id, String name, int age, boolean sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
分析问题:
当配置如下图则会出现JSTL、EL无法使用:
当配置如下图则JSTL、EL正常使用:
学习心得:各个版本尽量匹配
1.tomcat版本要 >= jdk版本
2.运行时jdk版本要 >= 编译时jdk版本
3. spring版本 >=Tomcat版本
较低版本的spring,可以在较高版本的jdk和tomcat中使用,较高spring版本无法在低版本Tomcat中使用。