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