开发者

Jquery dialog - based on json submitted to Spring Controller that returns a view

I have used this code as the basis of my development so far : Ajax Simplifications from sp开发者_C百科ringsource.

Here is the html & jquery/javascript code:

<c:url var="Controller" value="/ControllerUrl" />
...
var previewDialog = $("<div></div>").dialog({
    //all the dialog setttings
});

$(".opener").click(function() {
    previewDialog.load("${Controller}",function(data) {
          previewDialog.dialog('open');
       });
       return false;
});

And the controller :

@RequestMapping(value = "/ControllerUrl", method = RequestMethod.GET)
public String previewDialog(Model model) {
    MyClass myClass = new MyClass();
        myClass.setTitle("SUCCESS");
    model.addAttribute(myClass);
    return "dialogContent";
}

This is all almost working, except in dialogContent.jsp (which is indeed opened in my dialog) "SUCCESS" is not printed:

<div id="divContent">
Title : ${myClass.title} <br>   
</div>

What am I missing/doing wrong ?

Secondly, what is the besy way to submit json data to server in this context - I attempted using $.ajax() and $.postJSON() but ran into problems as they work differently to the $.load() statement.

Thanks in advance.


Can you try:

In the Controller:

return new ModelAndView("view-name", "myclass", myClass);

In your JSP:

${myClass.title}

The trouble is that this would till return HTML rather than just plain text.

Also, you can probably return json or xml by changing your controller as follows:

@RequestMapping(value = "/ControllerURL", method = RequestMethod.GET, headers="Accept=application/xml, application/json")
public @ResponseBody DealManager homeXmlJson(Locale locale, Model model) {

       MyClass myClass = new MyClass();
           myClass.setTitle("SUCCESS");

    return myClass;
}

Then when you call it using $.getJSON it should return a json representation of the object from which you should be able to extract the title.

Use a tool like REST-Client to see what is returned when you pass different Accept parameters to the controller URL. the parameter being: Accept: text/html, Accept: application/json, Accept: application/xml

You will have to configure your rest context as well. Here is an example of one I'm using:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter" />
                <ref bean="marshallingConverter" />
                <!-- <ref bean="atomConverter" /> -->
            </list>
        </property>
    </bean>

    <!-- Handle JSON Conversions -->
    <bean id="jsonConverter"
        class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json" />
    </bean>

    <!-- Handle XML Conversion -->
    <bean id="marshallingConverter"
        class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <constructor-arg ref="jaxbMarshaller" />
        <property name="supportedMediaTypes" value="application/xml" />
    </bean>

    <bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
        <property name="classesToBeBound">
            <list>
                <value>com.pack1.app.domain.MyEntity</value>
                <value>com.pack1.app.service.MyEntityTwo</value>
            </list>
        </property>
    </bean>


</beans>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜