【问题标题】:The requested resource is not available simple Spring MVC app请求的资源不可用简单的 Spring MVC 应用程序
【发布时间】:2016-08-06 00:43:42
【问题描述】:

我正在尝试创建一个简单的 Spring MVC 应用程序,但我不知道为什么会收到错误 404 The requested resource is not available。

这是xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

调度器服务:

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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-3.2.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">


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

    <context:component-scan base-package="controllers" />
    <mvc:annotation-driven />
</beans>

我的控制器只处理一个简单的模型:

@org.springframework.stereotype.Controller
public class Controller {

    @RequestMapping("/")
    public String getData(Model model) {
        model.addAttribute("message", "Hello World!");
        return "index";
    }
}

还有 JSP 文件!我只想打印来自控制器的消息。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <h1>${message}</h1>
  </body>
</html>

【问题讨论】:

  • 张贴项目图和你的网址

标签: java spring spring-mvc model-view-controller web


【解决方案1】:

这里试着说 ant 文件以 .jsp 结尾

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

尝试更改此方法并返回一个 ModelAndView 对象

 @RequestMapping("/")
 public ModelAndView getData() {
      ModelAndView mav = new ModelAndView("index");
      mav.addObject("message", "Hello World!");
      return mav;
 }

或者你仍然可以使用 String 作为返回类型:

 @RequestMapping("/")
 public String getData(Model model) {
      model.addAttribute("message", "Hello World!");
      return "index";
 }

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 2016-12-27
    • 2013-02-16
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多