SPRINGMVC (eight) return void of Springmvc return value type

Source: Internet
Author: User

SPRINGMVC return value type has medelandview,string,void,object numeric type, collection type, etc.

The first two have been used since we wrote the case, now look at the return value is void

If the return value is void, it should be written in conjunction with Ajax.

Prepare a user class

Package Demo09return;/** * Created by mycom on 2018/3/26.*/ Public classUserInfo {PrivateString username; PrivateString password;  PublicString GetUserName () {returnusername; }     Public voidSetusername (String username) { This. Username =username; }     PublicString GetPassword () {returnpassword; }     Public voidSetPassword (String password) { This. Password =password; }}

and controller classes

Package Demo09return;import Com.alibaba.fastjson.json;import org.springframework.stereotype.controller;import Org.springframework.web.bind.annotation.requestmapping;import Javax.servlet.http.httpservletresponse;import Java.io.ioexception;import Java.util.arraylist;import java.util.List;/** * Created by mycom on 2018/3/26.*/@Controller Public classReturncontroller {@RequestMapping ("/one")     Public voidDofirst (httpservletresponse response) throws IOException {List<UserInfo> list=NewArraylist<userinfo>(); UserInfo User=NewUserInfo (); User.setusername ("ao"); User.setpassword ("1225");        List.add (user); String Data=json.tojsonstring (list);    Response.getwriter (). write (data); }}

On the page

<%--Created by IntelliJ. User:mycom Date:2018/3/ -Time : -: - to change ThisTemplate Use File | Settings |File Templates.--%><%@ page contenttype="Text/html;charset=utf-8"Language="Java"Iselignored="false"%>"Text/javascript"Src="${pagecontext.request.contextpath}/js/jquery-1.8.3.min (1). js"></script><script type="Text/javascript">$ (function () {$ ("input"). Click (function () {$.ajax ({URL:"/one", type:"POST", Success:function (data) {$.each (eval ("("+data+")"), function (i,dom) {alert (dom.username); })                }            })        })    })</script>"Button"Value="Button"></body>

In the configuration 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:mvc="Http://www.springframework.org/schema/mvc"Xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/MVC http://www.springframework.org/schema/mvc/spring-mvc.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scanBase-package="Demo09return"></context:component-scan> <!--View Resolver-<beanclass="Org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"Value="/"></property> <property name="suffix"Value=". JSP"></property> </bean></beans>

Be careful not to forget to modify the value of Classpath in Web. xml

SPRINGMVC (eight) return void of Springmvc return value type

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.