我在eclipse indigo中创建了一个web Dyanamic项目,只是为了打印Hello World。但我正面临着HTTP状态404 - / HelloWorld /错误。
以下是你好。 JSP:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.0 MVC Series: Hello World</title>
</head>
<body>${message}
</body>
</html>
以下是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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>HelloWorld</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
下面是index.jsp的代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring 3.0 MVC Series: Index</title>
</head>
<body>
<a href="hello.html">Say Hello</a>
</body>
</html>
以下是HelloWeb-Sevlet.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"
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">
<context:component-scan
base-package="com.pkg.controller" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
我的src文件夹包含co.pkg.controller,它有一个java文件HelloWorldController.java:
package com.pkg.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings("unused")
public class HelloWorldController {
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
我在Webcontent / Web-Inf / lib文件夹中导入了下面提到的jar:
commons-logging-1.1.1.jar
jstl-1.2.jar
org.springframework.web.servlet-3.0.1.RELEASE-A.jar
spring-aop-3.0.1.RELEASE.jar
spring-asm-3.0.1.RELEASE.jar
spring-beans-3.0.1.RELEASE.jar
spring-context-3.0.1.RELEASE.jar
spring-core-3.0.1.RELEASE.jar
spring-expression-3.0.1.RELEASE.jar
spring-web-3.0.1.RELEASE.jar
我正在使用JBoss 6.x运行时服务器但错误是:
HTTP Status 404 - /HelloWorld/
--------------------------------------------------------------------------------
type Status report
message /HelloWorld/
description The requested resource (/HelloWorld/) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/6.0.29
请帮帮我。我不知道我在做什么错误。
答案 0 :(得分:0)
因为你使用的是spring 3.x web mvc。您需要对代码和配置进行一些更改。以下是您需要进行的更改
<mvc:annotation-deriven/>
标记。将@Controller注释放在控制器类的顶部,并在helloWorld()方法上指定RequestMapping。现在你的课程将如下所示
package com.pkg.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@SuppressWarnings("unused")
@Controller
public class HelloWorldController {
@RequestMapping(value="/HelloWorld")
public ModelAndView helloWorld() {
String message = "Hello World, Spring 3.0!";
return new ModelAndView("hello", "message", message);
}
}
确保WEB-INF / jsp /文件夹中有hello.jsp。
答案 1 :(得分:0)
人士Himanshu,
不要为注释驱动的条目流汗,因为在这种特殊情况下它实际上并不需要。
Raju几乎是正确的,但他给你的示例控制器中的@RequestMapping需要:
@RequestMapping(value="/hello")
要运行该示例,请在url中启动服务器后输入:
http://localhost:8080/YourEclipseProjectName/index.jsp
然后点击“如你好”&#39;链接以查看结果。如果您希望也可以跳过该步骤并直接输入网址:
http://localhost:8080/YourEclipseProjectName/hello.html
答案 2 :(得分:0)
我遇到了这个问题(Tomcat7 Eclipse Kepler),我终于在http://www.coderanch.com/t/606171/Tomcat/error-requested-resource-Eclipse找到了答案
基本上index.jsp必须在WebContent文件夹中,而hello.jsp必须在WEB-INF中