------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
处理器的方法我们之前做过,返回值为String的,返回值为ModelAndView的,我们这个讲的这个返回值void的做什么的?
答:最主要的是ajax的时候用的
案例如下:
俩个类:实体类UserInfo和处理器类:
UserInfo类:
package cn.dawn.day12return; /** * Created by Dawn on 2018/3/26. */ public class UserInfo { private String username; private String password; public UserInfo() { } public UserInfo(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
自定义的处理器类:
package cn.dawn.day12return; 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 Dawn on 2018/3/26. */ @Controller public class MyReturnDemoController { @RequestMapping("/doreturnVoidAjax") public void doreturnVoidAjax(HttpServletResponse response) throws IOException { List<UserInfo> lists=new ArrayList<UserInfo>(); UserInfo u1=new UserInfo("M","123"); UserInfo u2=new UserInfo("6","123"); lists.add(u1); lists.add(u2); String json = JSON.toJSONString(lists); response.getWriter().write(json); } }
此处用到了阿里的fastjson的jar包,自已引一个即可,不说这个jar了
自己的xml配置文件:
package cn.dawn.day12return; 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 Dawn on 2018/3/26. */ @Controller public class MyReturnDemoController { @RequestMapping("/doreturnVoidAjax") public void doreturnVoidAjax(HttpServletResponse response) throws IOException { List<UserInfo> lists=new ArrayList<UserInfo>(); UserInfo u1=new UserInfo("M","123"); UserInfo u2=new UserInfo("6","123"); lists.add(u1); lists.add(u2); String json = JSON.toJSONString(lists); response.getWriter().write(json); } }
在web.xml中把中央调度器调度到自己当前写的这个xml配置文件,再解决乱码,再解决静态资源js不能引用的问题
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--过滤器处理乱码--> <filter> <filter-name>CharacterEncoding</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> <!--响应--> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--中央调度器--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ApplicationContext-day12return.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--第一种方式--><!--解决静态资源不能引用--> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.gif</url-pattern> </servlet-mapping> </web-app>
jsp页面
<%@ page language="java" pageEncoding="utf-8" isELIgnored="false" %> <html> <head> <title>Ajax</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(function () { $("input").on("click",function () { $.ajax({ url:"${pageContext.request.contextPath}/doreturnVoidAjax", type:"POST", data:{}, success:function (data) { //data:String json串 data:Object json对象 //用户名 $.each(eval("("+data+")"),function (i,dom) { alert(dom.username); }); } }); }); }); </script> </head> <body> <%--<img src="image/1.jpg">--%> <input type="button" value="想发送Ajax?,点我不要赔的喔!!!呵呵哒~~~~~~"/> </body> </html>
案例完毕