java - Spring jsp页面未评估

我对 Spring 3.1.0 有一个有趣的问题。 JSP 页面没有评估其 EL。我已经调试了 View 解析过程,并且正在使用 JSTLView 并且检测到 JSTL 库。然而,我的 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>

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

Controller

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

我从 Controller 返回“example”作为 View 名称,您可以在日志中看到它解析为正确的 WEB-INF/jsp/example.jsp

22:35:13,049 DEBUG [1168657208@qtp-1342126426-0] [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 [1168657208@qtp-1342126426-0] [org.springframework.web.servlet.view.JstlView] Rendering view with name 'example' with model {} and static attributes {}
22:35:13,054 DEBUG [1168657208@qtp-1342126426-0] [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 jetty 6 插件来运行 webapp。

我确定我遗漏了一些简单的东西,这可能是那些“看得太久”的问题之一。

感谢您的指点。

Update 1 - 我刚刚将 Spring servlet 映射从 /* 更改为 /spring/*,现在它可以工作了。所以有一些关于 spring servlet 的细节被映射到 /* 我错过了。

最佳答案

绝对是“看了太久”的案例。

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>

https://stackoverflow.com/questions/11836641/

相关文章:

java - 我应该把我的 ThreadLocals 放在一个 Spring 注入(inject)的

java - 多语言数据库,默认回退

java - 关于 Spring web.xml

java - Spring MVC : How to return different type i

java - 非法状态异常 : Cannot find changelog location: cl

java - Spring 集成 - 入站与出站 channel 适配器

java - 依赖注入(inject) servlet 监听器

json - Spring 3.2RC1 中来自 JodaTime 的 jackson2 JSON

spring - 将 HTML5 占位符属性添加到 Spring 3.0 表单输入元素

java - Spring 3中@Component和@Configuration的区别