0

我是 spring mvc 框架的新手,并关注此站点以获取 hello world 教程

错误: 请求的资源不可用

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
    <display-name>HelloWorldExampleWithSpring3MVCInEclipse</display-name>
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/app-config.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

应用程序配置.xml

<?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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.raistudies" />
    <!-- Configures the @Controller programming model -->
    <mvc:annotation-driven />
    <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

HelloWorldAction.java

package com.raistudies.actions;

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

@Controller
public class HelloWorldAction {

    @RequestMapping(value="/hello",method=RequestMethod.GET)
    public ModelAndView sayHello(Model model){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        model.addAttribute("HelloMessage", "Hello World from My First Spring 3 mvc application");
        return mv;
    }
}

你好.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page session="true" %>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Hello World with Spring 3 MVC</title>
    </head>
    <body>
        <h1>Welcome! Spring MVC is working well.</h1><br />
        ${HelloMessage}
    </body>
</html>
4

3 回答 3

1

web.xml中,当我们将“*htm”替换为“/”时,资源不可用的错误就解决了。

于 2013-10-24T02:49:29.960 回答
1

最初的问题是因为您将所有请求都发送*.htm到 Spring:

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

但是您的控制器被映射到/hello没有扩展名。

将控制器中的更改@RequestMapping为:

@RequestMapping(value="/hello.htm",method=RequestMethod.GET)
于 2013-10-24T07:51:32.597 回答
0

如果您正在访问http://localhost:8080/HelloWorldExampleWithSpring3MVCInEclipse/,则需要在您的项目中创建一个 WebContent/index.jsp 以使该 URL 工作。

index.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
   pageEncoding="ISO-8859-1"%>
<jsp:forward page="/hello.htm"></jsp:forward>

请参阅此处的示例代码。

于 2013-10-23T00:41:04.893 回答