Spring mvc:Controller Result重新发送到RequestMappingHandlerMapping

时间:2015-10-25 00:54:06

标签: spring-mvc

我有一个由各种控制器扩展的抽象Spring Controller类。 示例方法:

@Override
@RequestMapping(value = { "/", "" }, method = RequestMethod.GET)
public String getAllAsView(@RequestParam(required = false) boolean ajax,
        Model m) {
    String mapping = elementClass.getSimpleName();
    m.addAttribute(mapping + "List", getAll());
    return mapping + "All" + (ajax ? "Ajax" : "");
}

这些是我的view.xml中的相关定义:

<definition name="maintemplate" template="/WEB-INF/views/main_template.jsp">
    <put-attribute name="top" value="/WEB-INF/views/header.jsp" />
    <put-attribute name="side" value="/WEB-INF/views/menu.jsp" />
</definition>
<definition name="ajaxtemplate" template="/WEB-INF/views/ajax_template.jsp">
    <put-attribute name="top" value="/WEB-INF/views/header.jsp" />
</definition>
<definition name="PersonAll" extends="maintemplate">
    <put-attribute name="content" value="/WEB-INF/views/personlist.jsp" />
</definition>
<definition name="PersonAllAjax" template="ajaxtemplate">
    <put-attribute name="content" value="/WEB-INF/views/personlist.jsp" />
</definition>

使用ajax参数时,只返回正文内容。

没有ajax参数,一切正常。 但是使用Ajax参数,返回字符串用于新的Controller请求。 这是日志:

DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /person/6
TRACE: org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Invoking [PersonController.getAsView] method with arguments [6, true, {}]
WARN : de.kreth.clubhelperbackend.aspects.DaoLoggerAspect - de.kreth.clubhelperbackend.dao.PersonDao.getById(6) ==> 6: M Kreth
TRACE: org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod - Method [getAsView] returned [PersonGetAjax]
DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /person/ajaxtemplate

这是servlet-context.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">

<context:component-scan base-package="de.kreth.clubhelperbackend" />

<mvc:annotation-driven>
    <mvc:message-converters>
        <bean
            class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
            <property name="objectMapper">
                <bean
                    class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                    <property name="dateFormat">
                        <bean class="java.text.SimpleDateFormat">
                            <constructor-arg type="java.lang.String"
                                value="dd/MM/yyyy HH:mm:ss.SSS Z"></constructor-arg>
                        </bean>
                    </property>
                </bean>
            </property>
        </bean>
    </mvc:message-converters>
</mvc:annotation-driven>

<mvc:resources mapping="/resources/**" location="/resources/" />

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver" />

<bean class="org.springframework.web.servlet.view.tiles2.TilesViewResolver">
    <property name="order" value="1" />
</bean>

<bean class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/views/**/views.xml</value>
        </list>
    </property>
</bean>

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

我很难发现这里发生了什么。我更改了模板名称,并将新名称用于请求。

在我更改了项目中的几乎所有xml文件后,出现了问题。我插入了Doctype标签并更改了模式定义和内容。因为这导致了严重的问题,我又回到了工作版本。在此之前,ajax参数有效。 啊 - 我更新到java-version 1.6。

为什么spring使用templatename“ajaxtemplate”作为新请求并将其发送回控制器的任何想法?

祝你好运 马库斯

1 个答案:

答案 0 :(得分:0)

愚蠢的错误:从未在午夜过后编程:

<definition name="PersonAllAjax" template="ajaxtemplate">
    <put-attribute name="content" value="/WEB-INF/views/personlist.jsp" />
</definition>

必须是

<definition name="PersonAllAjax" extends="ajaxtemplate">
    <put-attribute name="content" value="/WEB-INF/views/personlist.jsp" />
</definition>