Spring MVC无法正确显示文本

时间:2016-03-21 01:21:59

标签: java spring jsp spring-mvc

尝试从Spring MVC创建一个hello world页面。

这是我的/WeeklyPickems/src/main/webapp/WEB-INF/config/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:mvc="http://www.springframework.org/schema/mvc"
   xmlns:context="http://www.springframework.org/schema/context"

   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/mvc
   http://www.springframework.org/schema/mvc/spring-mvc.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

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

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

这是我的/WeeklyPickems/src/main/webapp/WEB-INF/web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <servlet>
    <servlet-name>WeeklyPickemsServlet</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>WeeklyPickemsServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  <display-name>Archetype Created W eb Application</display-name>
</web-app>

这是我的/WeeklyPickems/src/main/java/com/lucentdigital/controller/HelloController.java

package com.lucentdigital.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";

    }

}

导航到http://localhost:8080时,浏览器会正确呈现/WeeklyPickems/src/main/webapp/index.jsp,但导航到http://localhost:8080/WeeklyPickems/greeting.html,我用它显示404错误:

HTTP Status 404 -
type Status report
message
description The requested resource is not available.

现在我已经更新了我的PATH并且一切正常但仍未显示正确的“Hello World”。我在/WeeklyPickems/src/main/webapp/WEB-INF/jsp/hello.jsp

中有以下内容
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <h1>${greeting}</h1>
    </body>
</html>

该网页在页面上显示“$ {greeting}”字样。 我需要更改为在H1标记中实际显示“Hello World”的文件? 感谢。

2 个答案:

答案 0 :(得分:0)

您的servlet名称与您的servlet上下文无关。

我相信在这种情况下,根据您的问题,您的上下文为/

要做的第一件事是更改您的url-mapping

<servlet-mapping>
    <servlet-name>WeeklyPickemsServlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

<servlet-mapping>
    <servlet-name>WeeklyPickemsServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

在控制器中描述之后调用http://localhost:8080/greeting

答案 1 :(得分:0)

我通过更改isELIgnored="false"添加page来修复此问题。这是生成的jsp文件:

<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
      <title>Title</title>
    </head>
    <body>
        <h1>${greeting}</h1>
    </body>
</html>