Spring MVC JSTL Config ClassNotFoundException

时间:2017-11-05 19:35:40

标签: java spring spring-mvc tomcat

我有一个Spring MVC应用程序的简单示例。 gradle配置中只有两个依赖项 - springmvc和jstl。这些依赖项包含在WEB-INF / lib文件夹中的WAR文件中。但是在Tomcat上,当我尝试获取目标页面时,我得到了ClassNotFoundException。

Tomcat版本 - 8.5.16

项目结构:

src/main/java/net/wls/test/springmvc/HelloController.java
src/main/webapp/WEB-INF/jsp/hello.jsp
src/main/webapp/WEB-INF/servlet-context.xml
src/main/webapp/WEB-INF/web.xml
src/main/webapp/index.html
build.gradle

HelloController.java:

package net.wls.test.springmvc;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public ModelAndView hello() {
        String message = "Hello World!";
        return new ModelAndView("hello", "message", message);
    }
}

的hello.jsp:

<h1>${message}</h1>

servlet的context.xml中:

<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
    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">

    <context:component-scan base-package="net.wls.test"/>

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.1">

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/servlet-context.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

的build.gradle:

group 'net.wls.test'
version '0.1.0'

def springVersion = '5.0.0.RELEASE'

apply plugin: 'war'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    mavenLocal()
    jcenter()
}

task copyRuntimeLibs(type: Copy) {
    into 'lib'
    from configurations.runtime
}

war {
    classpath fileTree('lib')
}

dependencies {
    compile group: 'org.springframework', name: 'spring-webmvc', version: "$springVersion"
    compile group: 'javax.servlet.jsp.jstl', name: 'jstl', version: '1.2'
}

1 个答案:

答案 0 :(得分:0)

build.gradle 中的 JSTL 相关性更改为:

compile group: 'javax.servlet', name: 'jstl', version: '1.2'

应该出错。