2015-08-27 85 views
1

這個問題之前曾經被問過,但他只是說,他解決了這個問題,這對他的行爲來說是一個愚蠢的錯誤。這並沒有太大的幫助。這就是我的瀏覽器渲染:Spring MVC中的JSP渲染HTML

enter image description here

這裏是我的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也得到了相同的結果。我也檢查了它會多個瀏覽器。

*************************** NOTE ****************** *** 我加:

<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> 

----------------- upate 2 ------------------

@Controller 
public class IndexController { 

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

} 
+0

如何部署和運行應用程序? –

+0

intellij中的Tomcat – Drew1208

+0

向我們展示您的調度程序servlet.xml –

回答

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> 
+0

我覺得這很奇怪。我認爲/ *會導致web.xml尋找超出/的所有內容。你的解決方案奏效,我感謝你。 – Drew1208