What I want to do is to return a User json object from controller. But my code always goes to the error callback function (it displays "error function" in the browser when clicking on the button). Nothing in the log. Can anybody help? Thanks.
spring file:
[CODE]
<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
        
    <mvc:annotation-driven />
    <context:component-scan base-package="test"/>
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--
    <mvc:interceptors>
        <bean id="testInterceptor" class="test.web.TestInterceptor"></bean>
        <bean id="testInterceptor2" class="test.web.TestInterceptor2"></bean>
    </mvc:interceptors>
    -->
    
    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</beans>
[/CODE]jsp file:
[CODE]
<html>
<head>
    <title>Spring 3.0 Test</title>
    <script language="javascript" type="text/javascript" src="../jsp/jquery-1.4.2.min.js"></script>
    <script language="javascript" type="text/javascript" src="../jsp/json.min.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
$(document).ready( function() {
    $("#getJson").click(function(){
        alert("click");
        $.ajax({
            type: "POST",
            url: "http://localhost:8088/spring3/test/testJson.do",
            contentType: 'application/json',
            dataType: "json",
            data: {firstName: "Tom"},
            success: function(json) {
                alert("ok");
                alert(json);
                var txt = "name: " + json.firstName + " " + json.lastName + "\n" + "GID: " + json.gid;
                alert(txt);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("error function");
                alert(textStatus);
                alert(errorThrown);
            }
        });
    });
}
);
</script>Spring 3.0 is coming...
<br />
<form id="f1">
<input type="hidden" id="firstName" name="firstName" value="Tom" />
<input type="hidden" id="lastName" name="lastName" value="Cat" />
<input type="hidden" id="gid" name="gid" value="001" /></form>
<input type="button" id="getJson" value="Get User Info by JSON" />
</body>
</html>
[/CODE]Java Code:
[CODE]
package test.web;import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@RequestMapping("/test")
public class TestController {    @RequestMapping(value="/addUser.do", method = RequestMethod.GET)
    public String welcome(Model model) {
//        try {
//            String test = request.getParameter("test");
//            System.out.println(test);
//        }catch(Exception e){
//            System.out.println(e);
//        }
        User user = new User();
        model.addAttribute("user", user);
        
        return "addUser";
    }
    
    @RequestMapping(value="/addUser.do", method = RequestMethod.POST)
    public String addUser(@ModelAttribute("user") User user,BindingResult result) {
        try {
            System.out.println("first name: " + user.getFirstName());
            System.out.println("last name: " + user.getLastName());
        }catch(Exception e){
            System.out.println(e);
        }
        return "test";
    }
    
    @RequestMapping(value="/testJson.do", method=RequestMethod.POST)
    @ResponseBody
    public User getJsonUserInfo(@RequestParam String firstName) {
        try {
            System.out.println(firstName);
        } catch (Exception e) {
            System.out.println("getJsonUserInfo() - " + e);
        }
        User result = new User();
        result.setFirstName("Tester");
        result.setLastName("Tester");
        result.setGid("12345");
        JSONObject backJsonObj = new JSONObject(result);
        System.out.println(backJsonObj.toString());
        return result;
    }
}[/CODE]

解决方案 »

  1.   

    spring file:<?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: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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
            
        <mvc:annotation-driven />
        <context:component-scan base-package="test"/>
        
        <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
            <property name="prefix" value="/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        <!--
        <mvc:interceptors>
            <bean id="testInterceptor" class="test.web.TestInterceptor"></bean>
            <bean id="testInterceptor2" class="test.web.TestInterceptor2"></bean>
        </mvc:interceptors>
        -->
        
        <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
    </beans>
    jsp file:<html>
    <head>
        <title>Spring 3.0 Test</title>
        <script language="javascript" type="text/javascript" src="../jsp/jquery-1.4.2.min.js"></script>
        <script language="javascript" type="text/javascript" src="../jsp/json.min.js"></script>
    </head>
    <body>
    <script language="javascript" type="text/javascript">
    $(document).ready( function() {
        $("#getJson").click(function(){
            alert("click");
            $.ajax({
                type: "POST",
                url: "http://localhost:8088/spring3/test/testJson.do",
                contentType: 'application/json',
                dataType: "json",
                data: {firstName: "Tom"},
                success: function(json) {
                    alert("ok");
                    alert(json);
                    var txt = "name: " + json.firstName + " " + json.lastName + "\n" + "GID: " + json.gid;
                    alert(txt);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("error function");
                    alert(textStatus);
                    alert(errorThrown);
                }
            });
        });
    }
    );
    </script>Spring 3.0 is coming...
    <br />
    <form id="f1">
    <input type="hidden" id="firstName" name="firstName" value="Tom" />
    <input type="hidden" id="lastName" name="lastName" value="Cat" />
    <input type="hidden" id="gid" name="gid" value="001" /></form>
    <input type="button" id="getJson" value="Get User Info by JSON" />
    </body>
    </html>
    Java Code:package test.web;import org.json.JSONObject;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.validation.BindingResult;
    import org.springframework.web.bind.annotation.ModelAttribute;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;@Controller
    @RequestMapping("/test")
    public class TestController {    @RequestMapping(value="/addUser.do", method = RequestMethod.GET)
        public String welcome(Model model) {
    //        try {
    //            String test = request.getParameter("test");
    //            System.out.println(test);
    //        }catch(Exception e){
    //            System.out.println(e);
    //        }
            User user = new User();
            model.addAttribute("user", user);
            
            return "addUser";
        }
        
        @RequestMapping(value="/addUser.do", method = RequestMethod.POST)
        public String addUser(@ModelAttribute("user") User user,BindingResult result) {
            try {
                System.out.println("first name: " + user.getFirstName());
                System.out.println("last name: " + user.getLastName());
            }catch(Exception e){
                System.out.println(e);
            }
            return "test";
        }
        
        @RequestMapping(value="/testJson.do", method=RequestMethod.POST)
        @ResponseBody
        public User getJsonUserInfo(@RequestParam String firstName) {
            try {
                System.out.println(firstName);
            } catch (Exception e) {
                System.out.println("getJsonUserInfo() - " + e);
            }
            User result = new User();
            result.setFirstName("Tester");
            result.setLastName("Tester");
            result.setGid("12345");
            JSONObject backJsonObj = new JSONObject(result);
            System.out.println(backJsonObj.toString());
            return result;
        }
    }
      

  2.   

    I could not understand why you write title in Chinese but write content in English?
      

  3.   

    I could not understand why you write title in Chinese but write content in English?
    ______________________________________
    请问这位一直用中文写代码吗,小弟不解