2012-08-06 162 views
13

我對Spring 3.1.0有一個有趣的問題。 JSP頁面沒有對其EL進行評估。我調試了視圖解析過程,正在使用JstlView並檢測到Jstl庫。然而,我的JSP頁面只呈現之類的東西Spring jsp頁面未評估

<%="Hello World!"%> 

有很多關於這個問題,沒有一個爲我工作過的在這裏引用。

Script tags not rendered in JSP page (using Spring + Tiles + JSPX) Spring and JSP EL not being processed

從頂部,這裏是我的配置;

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<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">   

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/*</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
</web-app> 

彈簧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:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
    <!-- 
    https://stackoverflow.com/questions/3652090/difference-between-applicationcontext-and-spring-servlet-xml-in-spring 
    -->   
</beans> 

的applicationContext.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:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" 
xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd 
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com.csc.fs.emea" /> 
<mvc:default-servlet-handler /> 
<mvc:annotation-driven /> 
<mvc:resources mapping="/static/**" location="/" /> 

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" p:order="1" /> 
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" p:order="2" p:defaultErrorView="sorry" /> 

<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"> 
    <property name="templateLoaderPath" value="/WEB-INF/freemarker/" /> 
    <property name="freemarkerSettings"> 
     <props>    
      <prop key="default_encoding">UTF-8</prop> 
      <prop key="output_encoding">UTF-8</prop> 
      <prop key="auto_include">macros.ftl</prop> 
      <prop key="auto_import">spring.ftl as spring</prop> 
      <prop key="template_update_delay">${freemarker.template.update.delay}</prop> 
     </props> 
    </property> 
    <property name="freemarkerVariables"> 
     <props> 
      <prop key="googleAnalyticsId">${google.analytics.id}</prop> 
     </props> 
    </property> 
</bean> 

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> 
    <property name="viewResolvers"> 
     <list> 
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
       <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
       <property name="prefix" value="/WEB-INF/jsp/" /> 
       <property name="suffix" value=".jsp" /> 
       <property name="contentType" value="text/html;charset=UTF-8"></property> 
      </bean> 
      <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> 
       <property name="cache" value="true" /> 
       <property name="prefix" value="" /> 
       <property name="suffix" value=".html" /> 
       <property name="contentType" value="text/html;charset=UTF-8"></property> 
       <property name="exposeSpringMacroHelpers" value="true" /> 
      </bean>    
     </list> 
    </property>  
</bean> 

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
</bean> 
<bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
    <property name="paramName" value="lang" /> 
</bean> 
<bean id="localeResolver" class="org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver"></bean>  
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> 
    <property name="interceptors"> 
     <ref bean="localeChangeInterceptor" /> 
    </property> 
</bean> 
</beans> 

我使用ContentNegotiatingViewResolver,因爲我有一些freemarker的東西和REST。

控制器

@RequestMapping("/") 
@Controller 
public class RootResource extends AbstractResource { 

    ...abridged... 

    @RequestMapping(value="/jsp", method = RequestMethod.GET, produces = "text/html") 
    public String getJSP(final Model m) {   
     return "example"; 
    } 
} 

example.jsp

<%@ page isScriptingEnabled="true" isELIgnored="false" %> 
<html> 
<head> 
    <title>Hello World JSP Page.</title> 
</head> 
<body> 
<font size="10"><%="Hello World!"%></font> 
</body> 
</html> 

我回來「榜樣」作爲視圖名從我的控制器,你可以在它解析爲正確的日誌中看到WEB-INF/jsp/example.jsp

22:35:13,049 DEBUG [[email protected]] [org.springframework.web.servlet.view.ContentNegotiatingViewResolver] Returning [org.springframework.web.servlet.view.JstlView: name 'example'; URL [/WEB-INF/jsp/example.jsp]] based on requested media type 'text/html' 
22:35:13,050 TRACE [[email protected]] [org.springframework.web.servlet.view.JstlView] Rendering view with name 'example' with model {} and static attributes {} 
22:35:13,054 DEBUG [[email protected]] [org.springframework.web.servlet.view.JstlView] Forwarding to resource [/WEB-INF/jsp/example.jsp] in InternalResourceView 'example' 

因此,一切看起來都不錯,只是JSP頁面從未被正確計算。

example.jsp看起來像這樣

<%@ page isScriptingEnabled="true" isELIgnored="false" %> <%="Hello World!"%> 

我使用Maven的碼頭6插件運行web應用程序。

我確定我錯過了一些簡單的東西,它可能是那些「看着它太久」的問題之一。

謝謝你可以給的任何指針。

更新1 - 我只是改變了春天的servlet映射從/*/spring/*和現在的作品。因此,Spring servlet的一些細節被映射到我錯過的/*

+2

初學者'<%=「Hello World!」%>'不是EL語法。 EL會閱讀'$ {「Hello World」}' – 2012-08-06 22:10:27

+0

謝謝克里斯 - 我一直在擺弄example.jsp,並嘗試過你以前的建議。仍然沒有快樂。 – biddster 2012-08-06 22:13:49

+0

對不起,我應該澄清,我沒有想到它解決了你的問題(對不起!)這只是一個評論;-) – 2012-08-06 22:14:27

回答

32

絕對是一個「看着它太久」的例子。

spring servlet需要成爲默認的servlet。即映射到/而不是/*

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>spring</servlet-name> 
    <url-pattern>/</url-pattern>   
</servlet-mapping> 
+1

絕對適合的解決方案。我調試了ContentNegotiatingViewResolver,它工作得很好。無可否認,我仍然無法理解爲什麼它會導致它僅僅因爲通配符而渲染原始JSP。你能否詳細說明一下?非常感謝。 – 2014-10-05 14:17:44

+0

/將此servlet作爲應用程序的默認servlet,但/ *只是一個通配符映射。 – 2015-12-06 09:29:47

+0

非常感謝!我正要生氣。 – asgs 2016-01-03 10:04:50