2016-08-25 3 views
1

Я получаю ошибку 404, когда я пытаюсь открыть 'greeting.html'Spring контроллер не работает

Ниже перечислены файлы:

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 

    <display-name>Archetype Created Web Application</display-name> 

    <servlet> 
     <servlet-name>fitTrackerServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/config/servlet-config.xml</param-value> 
     </init-param> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>fitTrackerServlet</servlet-name> 
     <!-- mapped all incoming requests to html --> 
     <url-pattern>*.html</url-pattern> 
    </servlet-mapping> 

</web-app> 

servlet-config.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" xmlns:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 

    <mvc:annotation-driven /> 
    <context:component-scan base-package="com.rahul.controller" /> 

    <!-- mentions where all the JSPs are located at --> 
    <bean 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp" p:suffix=".jsp"></bean> 

</beans> 

HelloController.java

package com.rahul.controller; 

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

@Controller 
public class HelloController { 

    @RequestMapping(value = "/greeting") 
    public String sayHello(Model model) { 
     model.addAttribute("greeting", "hello world"); 
     return "hello"; 
    } 

} 

hello.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!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>Insert title here</title> 
</head> 
<body> 

    <h1>${greeting}</h1> 

</body> 
</html> 

Мой http request является GET localhost:8080/FitnessTracker/greeting.html

Что я делаю неправильно?

+1

Что вы 'http' запрос? – xenteros

+0

@xenteros http: // localhost: 8080/FitnessTracker/greeting.html – reiley

+0

'http: // localhost: 8080/fitTrackerServlet/greeting.html'? –

ответ

1

Попробуйте следующий рефакторинг:

@Controller 
public class HelloController { 

    @RequestMapping(value = "/greeting") 
    public Model sayHello() { 
     model.addAttribute("greeting", "hello world"); 
     return model; 
    } 
} 

Или запрос:

http://localhost:8080/fitTrackerServlet/gree‌​ting.html

+0

Нет. не работает. – reiley

+0

Вы проверили без '.html'? Вы получили 404? – xenteros

+0

Еще нет. Проверено с помощью & .html: 'http: // localhost: 8080/greeting' & 'http: // localhost: 8080/greeting.html' – reiley

Смежные вопросы