开发者

Spring 3.0 and jackson: posting a composite entitie with ajax+json

well i'm working with spring 3.0 in conjunction with ajax and JSON, so i've the next domain class:

public class WebSite {
int id;
String description;
SimpleType type;

    //getters and setters ...
    //...
}

The SympleType class is:

public class SimpleType {
int id;

    //getter and setters...
    //...
}

So i created a controller for my first domain class and it goes like:

@Controller
@RequestMapping(value="/web")
public class PaginaWebController {

    @RequestMapping(method=RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute(new WebSite());
        return "web/addWeb";
    }

    @RequestMapping(method=RequestMethod.POST)
    public @ResponseBody Map<String, ? extends Object> create(@RequestBody WebSite webSite, HttpServletResponse response) {
        System.out.println("I'm here");
        return null;
    }
}

In my 'addWeb' view i've the following:

<%@ page session="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

<html>
    <head>
        <title>Create Web</title>
        <script type="text/javascript" src="<c:url value="/resources/jquery-1.4.min.js" /> "></script>
        <script type="text/javascript" src="<c:url value="/resources/json.min.js" /> "></script>
    </head>
    <body>
        <h1>
            Create Web
        </h1>
        <form:form modelAttribute="paginaWeb" action="web" method="post">
            <fieldset>      
                <legend>Account Fields</legend>
                <p>
                    <form:label id="idLabel" for="id" path="id" cssErrorClass="error">ID</form:label><br/>
                    <form:input path="id" /><form:errors path="id" />
                </p>
                <p> 
                    <form:label for="descripcion" path="descripcion" cssErrorClass="error">descripcion</form:label><br/>
                    <form:input path="descripcion" /><form:errors path="descripcion" />
开发者_如何学Go                </p>

                <p> 
                    <form:label for="tipo" path="tipo" cssErrorClass="error">Tipo:</form:label><br/>
                    <form:input path="tipo" /><form:errors path="tipo" />
                </p>

                <p> 
                    <input id="create" type="submit" value="Create" />
                </p>
            </fieldset>
        </form:form>
    </body>

    <script type="text/javascript"> 
        $(document).ready(function() {
            $("#paginaWeb").submit(function() {
                var account = $(this).serializeObject();
                $.postJSON("web", account, function(data) {
                    //some useful code goes here...
                    //...
                });
                return false;               
            });
        });
    </script>
</html>

So, i'm posting the form to the controller using JSON, but it doesn't work, when i'm press submit i get next error:

WARNING: StandardWrapperValve[Spring MVC Dispatcher Servlet]: PWC1406: Servlet.service() for servlet Spring MVC Dispatcher Servlet threw exception
        org.codehaus.jackson.map.JsonMappingException: Can not construct instance of org.springframework.samples.mvc.ajax.account.SimpleType, problem: no suitable creator method found
         at [Source: org.apache.catalina.connector.CoyoteInputStream@f624f7; line: 1, column: 33]
            at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:160)
            at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:214)
            at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromString(BeanDeserializer.java:533)
            at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:355)
    ...

Anyone can help me please? i'm really stuck in this one. Any help will be really appreciated.

Cheers!


Make sure you have jackson-core-asl and jackson-mapper-asl on your classpath. If you use maven set something like this on your pom.xml

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-core-asl</artifactId>
  <version>${jackson.version}</version>
</dependency>

<dependency>
  <groupId>org.codehaus.jackson</groupId>
  <artifactId>jackson-mapper-asl</artifactId>
  <version>${jackson.version}</version>
</dependency>

I noticed though that you are explicitly returning null on your create method. Please double check.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜