springMVC json

     Now, in web page development, AJAX partial update is essential. In springMVC, it provides developers with a concise mechanism (JSON, xml) to realize the interaction of different data formats. SpringMVC mainly uses type converters (mrssageConverters) to convert the foreground The information is converted into the format required by the developer. Then add the @requestBody annotation before the corresponding Controller method receives the parameters to perform data conversion, or add the @ResponseBody annotation to the return value type of the method to convert the returned information into data in the relevant format.

1. Guide package

  jackson-core-asl.jar

  jackson-mapper-asl.jar

 

2. Write the Controller method

    @Controller
    public class JsonController {

     @RequestMapping(value="json.action")

     @ResponseBody
     public  String jsonController(@RequestBody String data){
             System.out.println(data);
            return data;
     }
    }

3. Write the jsp file

<textarea id="jsonArea" placeholder="请输入json文本" style="width: 300px; height: 200px;"></textarea><br />
<button onclick="submitMsg()">提交</button><br>
<div id="area"></div>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function submitMsg(){
         var value = $("#jsonArea").val();
         $.ajax({
              type:"post",
              url:"json.action",
              contentType:"application/json;charset=utf-8",
              data:value,
              success:function(data){
              alert(data);
              $("#area").html(data);
             }
         });
     }
</script>

Here, because the jq framework is used, the jq package needs to be connected. One thing to note here is that when we usually develop, our configuration files will not be placed directly in the src path, but a new config will be created. The folder stores the configuration file, and the configuration file is initialized in web.xml, that is, it is added to the servlet tag in web.xml

<init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
</init-param>

It will not be able to connect to css, or jq, or image files. At this time, we need to add in the web.xml file

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>


<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>

You can connect to js or css files, and the same is true for pictures.

The last one is that because the default encoding used by spring is ISO-8859-1, when json responds to a web page, the web page receives a json string with Chinese garbled characters, which is added in the springMVC configuration file.

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="utf-8"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

Can solve the garbled problem

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325075718&siteId=291194637