java Spring MVC——请求的资源()不可用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11488443/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC -- requested resource() not available
提问by Jerome Dalbert
I am working on a Spring 3.1 MVC application, and I have a JSP that is not rendering at all in my browser. Instead, I see a message that says, "The requested resource () is not available." Can anyone help? Here is my error message.
我正在开发一个 Spring 3.1 MVC 应用程序,我有一个 JSP,它根本没有在我的浏览器中呈现。相反,我看到一条消息“请求的资源 () 不可用”。任何人都可以帮忙吗?这是我的错误信息。
HTTP Status 404 -
--------------------------------------------------------------------------------
type Status report
message
description The requested resource () is not available.
This is GuestBook.jsp.
这是GuestBook.jsp。
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="myTag" uri="commentsTag"%>
<html>
<head>
<title>Granada High School Class of 1986 Comment Page</title>
<!-- Put a confirmation message here once you
figure out how to do it with Spring -->
<style type="text/css">
p {
text-align: justify;
}
h1 {
text-align: center;
}
</style>
</head>
<body>
<h1>Granada High School Class of 1986 Guest Book</h1>
<%@ include file="menu.jsp"%><br>
<b><font color="red"><c:out value='${confirmation}' /></font></b>
<c:remove var="confirmation" scope="session" />
<p>This page is a place where you can share memories from high
school, what you're doing these days, comments about this web site
(good or bad), etc. The comments will appear at the bottom of the
page.</p>
<form:form method="post" action="GuestBook.jsp" modelAttribute="comment">
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="message">Message</form:label></td>
<td><form:textarea path="message" rows="10" cols="50" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit comment" /></td>
<td> </td>
</tr>
</table>
</form:form>
<br>
<myTag:comments />
<%@ include file="footer.jsp"%>
</body>
</html>
This is com.controller.CommentController.java.
这是 com.controller.CommentController.java。
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.dao.CommentDAO;
import com.model.Comment;
@Controller
@RequestMapping("/GuestBook.jsp")
public class CommentController {
Comment comment;
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
@RequestMapping(value = "/GuestBook", method = RequestMethod.POST)
public String addComment(@ModelAttribute("comment") Comment comment, BindingResult result) {
CommentDAO commentDAO = new CommentDAO();
java.util.Date today = new java.util.Date();
java.sql.Date date = new java.sql.Date(today.getTime());
comment.setDate(date);
if (commentDAO.writeComment(comment) == true) {
return "redirect:GuestBook.jsp";
}
return "redirect:Oops.jsp";
}
@RequestMapping("/GuestBook")
public ModelAndView showComments() {
return new ModelAndView("comments", "command", new Comment());
}
}
This is web.xml.
这是 web.xml。
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>Granada High School Class of 1986</display-name>
<servlet>
<servlet-name>ghs1986</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ghs1986</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>com.configuration.ConfigurationListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
This is applicationContext.xml.
这是 applicationContext.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:p="http://www.springframework.org/schema/p"
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">
<mvc:annotation-driven />
<context:component-scan base-package="com.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
This is ghs1986-servlet.xml.
这是 ghs1986-servlet.xml。
<?xml version="1.0" ?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="comment" class="com.model.Comment" />
<bean name="/GuestBook.jsp" class="com.controller.CommentController">
<property name="comment" ref="comment" />
</bean>
</beans>
回答by Jerome Dalbert
Try the following :
尝试以下操作:
1) In web.xml, change
1)在web.xml中,更改
<url-pattern>/*</url-pattern>
to
到
<url-pattern>/</url-pattern>
2) move the code
2)移动代码
<mvc:annotation-driven />
<context:component-scan base-package="com.controller" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
from applicationContext.xml to ghs1986-servlet.xml
从 applicationContext.xml 到 ghs1986-servlet.xml
3) In CommentController, change @RequestMapping("/GuestBook.jsp")
to @RequestMapping("/GuestBook")
3) 在 CommentController 中,更改@RequestMapping("/GuestBook.jsp")
为@RequestMapping("/GuestBook")
4) Still in CommentController, change
4)还是在CommentController,改
@RequestMapping("/GuestBook")
public ModelAndView showComments() {
...
}
to something like
像
@RequestMapping("/showComments")
public ModelAndView showComments() {
...
}
Now try again with a url like http://localhost:<your_port>/GuestBook/showComments
. You should have access to your page. There may have other errors, but we are only dealing with the 404 one here.
现在再试一次像http://localhost:<your_port>/GuestBook/showComments
. 您应该有权访问您的页面。可能还有其他错误,但我们这里只处理 404 错误。
5) Optional (but strongly recommended) : read some doc + a good getting started tutorialto learn the basic best practises and how to set things up.
5) 可选(但强烈推荐):阅读一些文档 + 一个好的入门教程来学习基本的最佳实践以及如何设置。