【问题标题】:JSP rendering HTML in Spring-MVC在 Spring-MVC 中 JSP 渲染 HTML
【发布时间】:2015-08-27 15:45:40
【问题描述】:

以前有人问过这个问题,但他只是说,他解决了这个问题,这是他行为上的一个愚蠢的错误。这并没有太大帮助。这是在我的浏览器中呈现的内容:

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>SpringMVCpractice</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

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

    <!-- ========================================================== -->
    <!-- JSP Configuration -->
    <!-- ========================================================== -->

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <include-prelude>/WEB-INF/jsps/</include-prelude>
            <include-coda>/WEB-INF/jsps/</include-coda>
        </jsp-property-group>
    </jsp-config>

    <welcome-file-list>
        <welcome-file></welcome-file>
    </welcome-file-list>

</web-app>

index.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>Welcome</title>
</head>
<body>
Hello World...
</body>
</html>

我正在使用 intellij,但我也使用 Eclipse 进行了检查,得到了相同的结果。我还检查了它是否适用于多个浏览器。

***************************注意******************* ** 我补充说:

<jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <include-prelude>/WEB-INF/jsps/</include-prelude>
            <include-coda>/WEB-INF/jsps/</include-coda>
        </jsp-property-group>
    </jsp-config>

经过一番阅读,仍然得到了结果。即使它不存在,也会发生同样的事情。

-------更新-----------------

调度程序-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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.2.xsd">

    <mvc:annotation-driven></mvc:annotation-driven>

    <context:component-scan base-package="com.practice.comtroller"></context:component-scan>

    <mvc:default-servlet-handler />

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

</beans>

--更新2------------------

@Controller
public class IndexController {

    @RequestMapping({"/","index"})
    public String index(Model model){
        return "index";
    }

}

【问题讨论】:

  • 你如何部署和运行你的应用程序?
  • 向我们展示您的 dispatcherServlet.xml
  • 我认为你的Tomcat不知道如何渲染jsp,在classpath中添加jstl jar,更多信息:*.com/questions/20602010/…
  • @RaphaelAmoedo 请参阅更新 1

标签: java jsp spring-mvc


【解决方案1】:

解决了。首先:删除 jsp-config 和主要问题:您映射到 /* 而不是 / 。只要做这个改变,一切都会奏效。所以你的 web.xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>SpringMVCpractice</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

  <servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>

【讨论】:

  • 我觉得这很奇怪。我认为 /* 会导致 web.xml 查找 / 之外的所有内容。您的解决方案有效,我感谢您。