2013-12-09 42 views
0

我试过简单的你好世界程序使用spring.It不按预期工作。春天你好,世界不工作404错误

package com.Spring; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class Hello { 

    @RequestMapping(value="/hello",method=RequestMethod.GET) 
    public String helloWorld(ModelMap model){ 
     model.addAttribute("message", "hello world"); 
     return "hello"; 

    } 

} 

HelloWeb-servlet.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-2.5.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-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.Spring" /> 

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

</beans> 

的web.xml

<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"> 
    <servlet> 
    <servlet-name>HelloWeb</servlet-name> 
    <servlet-class>com.Spring.Hello</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>HelloWeb</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value> 
    </context-param> 
     <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
</web-app> 

的hello.jsp文件。

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<!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=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    ${message} 
</body> 
</html> 

我已经使用Ant生成WAR文件并部署deployed.After war文件没有在terminal.Now我有尝试在浏览器访问URL没有错误时显示404错误。

http://ipaddress:8080/SpringHello/hello.jsp 

如何访问URL?或者我做错了什么?

+1

尝试:http:// ipaddress:8080/SpringHello/hello。 – gregory561

+0

谢谢reply.yes.I已尝试不使用.jsp.Again它显示相同的错误404. – Ami

+0

请确保您的jsp和Web-servlet.xml位于WEB-INF下的jsp文件夹中。它不起作用,请尝试从Web-servlet.xml和web.xml中删除WEB-INF条目,并将jsp和Web-servlet.xml放入webapps文件夹direclty中。 – jsjunkie

回答

0

您正在web.xml中添加错误的servlet类。

<servlet-class>com.Spring.Hello</servlet-class> 

它应该是:

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 

更多的了解:http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/

+0

毫无疑问,互联网上最好的教程! –

+0

Thanks.I已经更改了servlet类并部署了war.It显示警告:在名为'HelloWeb'的DispatcherServlet中没有找到具有URI [/ SpringHello/hello/hello]的HTTP请求的映射。 – Ami

+0

asif..Yup true。! @ ami..Try with以下uri:http:// ipaddress:8080/SpringHello/hello // –

1

的处理器映射是在上下文文件中丢失了。因此,在Kamlesh建议的基础上添加注释驱动的bean。

<mvc:annotation-driven />