【问题标题】:Class not found Spring MVC找不到类 Spring MVC
【发布时间】:2015-01-15 15:17:53
【问题描述】:

我安装了 intellij idea 14 和 tomcat 8,并使用默认模板创建了一个 Spring MVC 项目。

我没有更改任何内容,只是在 IDE 中配置了我的 tomcat 路径。

我尝试运行我在事件日志中看到编译成功完成的默认应用程序,但是如果我转到 localhost:8080 我会收到此错误:

http://pastebin.com/cHgw4G0K

我的服务器、应用程序或 IDE 配置有什么问题?

这是我的源代码:

HelloController.java

@Controller
@RequestMapping("/")
public class HelloController {
        @RequestMapping(method = RequestMethod.GET)
        public String printWelcome(ModelMap model) {
                model.addAttribute("message", "Hello world!");
                return "hello";
        }
}

mvc-dispatcher-servlet.xml

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

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

web.xml

<web-app 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>Spring MVC Application</display-name>

    <servlet>
                <servlet-name>mvc-dispatcher</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        </servlet>

        <servlet-mapping>
                <servlet-name>mvc-dispatcher</servlet-name>
                <url-pattern>/</url-pattern>
        </servlet-mapping>
</web-app>

你好.jsp

<html>
<body>
        <h1>${message}</h1>
</body>
</html>

为什么我得到了 class not found 异常?

【问题讨论】:

  • 请将相关代码和堆栈跟踪添加到问题而不是链接。
  • pages目录下有没有叫hello.jsp的页面??
  • 这是我的项目结构:s27.postimg.org/5ez57zwz7/intellisource.png
  • Magnilex 我更新了我的问题!
  • 错误提示tomcat没有编译JSP文件……看起来部署不正确或不完整。您应该尝试停止 tomcat,进行干净的构建,部署并重新启动 tomcat。我不使用 IDEA,但应该有命令来做这样的事情......

标签: java spring spring-mvc tomcat intellij-idea


【解决方案1】:

我弄错了,您在 .jsp 文件中缺少 jstl lib 目录,这就是您的 jsp 给出错误的原因。

为了使用${message},你必须在你的jsp中包含jstl,比如 &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;

这会解决你的问题:)

【讨论】:

    【解决方案2】:

    您必须在 pom.xml 中更新 javax.servlet-api 的版本。 只需替换此依赖项:

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
    </dependency>
    

    到这里:

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 2015-03-31
      • 2018-07-06
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多