【问题标题】:Spring controller not working弹簧控制器不工作
【发布时间】:2016-08-25 10:23:36
【问题描述】:

当我尝试打开“greeting.html”时遇到错误 404

以下是文件:

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 requestGET localhost:8080/FitnessTracker/greeting.html

我做错了什么?

【问题讨论】:

标签: java spring jsp servlets


【解决方案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

【讨论】:

  • 您是否在没有.html 的情况下进行了检查?你得到 404 了吗?
  • 仍然没有。使用和不使用 .html 进行检查:'localhost:8080/greeting' & 'localhost:8080/greeting.html'
  • @reiley 尝试当前答案
  • 找到了...它缺少来自p:prefix="/WEB-INF/jsp/" 的“/”。谢谢你的帮助!!
  • 不错。很高兴您找到了解决方案。
猜你喜欢
  • 2016-12-23
  • 1970-01-01
  • 2014-10-28
  • 2014-08-27
  • 1970-01-01
  • 2022-09-23
  • 1970-01-01
  • 2014-12-14
相关资源
最近更新 更多