2013-12-09 3 views
0

Я пробовал простую программу Hello Hello, используя 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> 

Я создал архив войны с помощью муравьев и deployed.After развернут военный файл не существует ошибка в terminal.Now я есть пытаются получить доступ к URL в браузере он показывает сообщение об ошибке 404.

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

Как получить доступ к URL-адресу? или что я сделал неправильно?

+1

Попытка: http: // ipaddress: 8080/SpringHello/привет. – gregory561

+0

Спасибо за ответ. Я пробовал без использования .jsp.Again, он показывает ту же ошибку 404. – Ami

+0

Убедитесь, что ваш jsp и web-servlet.xml находятся в папке jsp под WEB-INF. Это не работает, попробуйте удалить записи WEB-INF из Web-servlet.xml и web.xml и поместите jsp и web-servlet.xml в папку webapps direclty. – jsjunkie

ответ

0

Вы добавляете неправильный класс сервлета в web.xml.

<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

Спасибо. Я изменил класс сервлета и развернул war.It показывает WARNING: Не найдено сопоставления для HTTP-запроса с URI [/ SpringHello/hello/hello] в DispatcherServlet с именем «HelloWeb». – Ami

+0

asif..Yup true.! @ ami..Try со следующими uri: http: // ipaddress: 8080/SpringHello/hello // –

1

HandlerMapping отсутствует в файле контекста тоже. Поэтому добавьте обработчик, основанный на аннотации, поверх того, что предложил Камлеш.

<mvc:annotation-driven /> 
Смежные вопросы