0

I have an abstract Spring Controller class extended by various controllers. Example method:

@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" : "");
}

These are the relevant definitions in my 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>

With the ajax parameter only the body content is to return.

Everything works fine without the ajax parameter. But with the Ajax parameter the return string is used for a new Controller request. This is the Log:

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

This is the 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>

I am stuck finding out what is happening here. I changed the template name and the new name was used for the request.

The problem came up, after I changed nearly all xml files in project. I inserted Doctype tag and changed schema definitions and stuff. Because that caused heavy problems I stepped back to a working version. Before that, the ajax parameter worked. Ah - and I updated to java-version 1.6.

Any ideas why spring uses the templatename "ajaxtemplate" as a new request and sends it back to the controller?

Best regards Markus

1 Answer 1

0

Stupid mistake: never programm past midnight:

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

must be

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.