Rookie Learning Spring--springmvc Note version gets the JSON string on the server side and parses

Source: Internet
Author: User

I. Overview.
SPRINGMVC A JSON string that is passed to the client on the server and turns the JSON string into a JSON object and obtains its property values, which are often used in the project.
second, the code demonstration.

The jar package that needs to be added.


2.1 Web. Xml.

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee/http Xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd "id=" webapp_id "version=" 3.1 "><filter><filter-name> Encodingfilter</filter-name><filter-class>org.springframework.web.filter.characterencodingfilter </filter-class><init-param><param-name>encoding</param-name><param-value>utf-8 </param-value></init-param></filter><filter-mapping><filter-name>encodingfilter </filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet>< Servlet-name>springmvc</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><load-on-startup>1</ Load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.spring</url-pattern></servlet-mapping>< Welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app >


2.2springmvc-servlet.xml.

<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Mvc= "Http://www.springframework.org/schema/mvc" 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 "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.1.xsd "><context:component-scan base-package=" Com.gaowei.JSON "/><mvc:annotation-driven/></beans>


2.3 jsp file.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
  

2.4getjsonstring.java.

Package Com.gaowei.json;import Net.sf.json.jsonobject;import Org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RequestParam; @Controllerpublic class Getjsonstring {@RequestMapping (value = "getjsonstring") public string getjsonstring (@RequestParam ("jsonstring") string jsonstring) {Jsonobject object= Jsonobject.fromobject (jsonstring); System.out.println (Object.get ("username")); System.out.println (Object.get ("password")); return "test.jsp";}}


2.5.


Iii. Summary

The interaction between page and Contorller layer avoids using JSON to pass value to the backstage, this method can let us better implement interface layer and Contorller interaction.

Rookie Learning Spring--springmvc Note version gets the JSON string on the server side and parses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.