簡體   English   中英

找不到類Spring MVC

[英]Class not found Spring MVC

我安裝了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-調度-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>

為hello.jsp

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

為什么我的課程找不到異常?

您必須在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>

我錯了,您在.jsp文件中缺少jstl lib目錄,這就是您的jsp給出error的原因。

要使用${message} ,必須在您的jsp中包含jstl,例如<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

這將解決您的問題:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM