<body> <!--返回值是string的内部视图 --> <a href="student/add">add</a> <!--返回值是string的外部视图 --> <a href="student/taobao">淘宝</a> <!--没有返回值 转发到内部视图 --> <a href="student/request">request</a> <!--没有返回值 重定向到内部视图 --> <a href="student/response">response</a> </body>
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置springmvc的组件 --> <context:component-scan base-package="cn.bdqn.controller"/> <!-- 视图解析器 后台返回的是 success! 应该拿到的是 /WEB-INF/jsp/success.jsp <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> --> <!-- 配置外部视图解析器 就不需要 视图解析器了! --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <!-- 外部视图 --> <bean id="taobao" class="org.springframework.web.servlet.view.RedirectView"> <property name="url" value="http://www.taobao.com"/> </bean> </beans>
@Controller @RequestMapping("/student") public class StudentController { /** * 返回值是string的内部视图 */ @RequestMapping("/add") public String add(){ System.out.println("进入了add"); return "success"; } /** * 返回值是string的外部视图 */ @RequestMapping("/taobao") public String taobao(){ System.out.println("进入了taobao"); return "taobao"; } /** * 没有返回值 转发到内部视图 */ @RequestMapping("/request") public void request(HttpServletRequest request,HttpServletResponse response){ System.out.println("进入了request"); //转发 try { request.getRequestDispatcher("/WEB-INF/jsp/success.jsp").forward(request, response); } catch (ServletException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 没有返回值 重定向到内部视图 */ @RequestMapping("/response") public void response(HttpServletRequest request,HttpServletResponse response){ System.out.println("进入了response"); //重定向 try { /** * 重定向是 客户端的 行为! * 能直接访问 web-inf ??? 不行! * response.sendRedirect("WEB-INF/jsp/success.jsp"); * 重定向到另一个方法 然后 那个方法做跳转! * */ response.sendRedirect("success.jsp"); //前提是根目录下有 success.jsp /** * 这时候的路径http://localhost:8080/springmvc08/student/success.jsp * 为什么会有student/这一层目录 * 原因: * 用户点击的是 student/response * 系统会默认拿到student/ 当成目前的路径! * 再做重定向的时候会默认加上student/ */ } catch (IOException e) { e.printStackTrace(); } } }
<body> <!-- 在controller中 实现 方法之间的跳转 --> <form action="student/changeMethod" method="post"> 学生姓名:<input type="text" name="name"/> 年龄: <input type="password" name="age"/> <input type="submit" value="新增"/> </form> </body>
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 配置springmvc的组件 --> <context:component-scan base-package="cn.bdqn.controller"/> <!-- 视图解析器 后台返回的是 success! 应该拿到的是 /WEB-INF/jsp/success.jsp --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
@Controller @RequestMapping("/student") public class StudentController { /** * 转发是默认的格式 * 01.看路径 * 02.看数据是否传递 */ @RequestMapping("/addRequest") public ModelAndView addRequest(Student student){ System.out.println("进入了addRequest"); ModelAndView mv=new ModelAndView(); mv.addObject("student", student).setViewName("success"); return mv; } /** * 重定向 * 01.客户端行为 不能访问WEB-INF * 02.以访问的路径为准,所以上个例子中的student/ 会出现! * 03.如果使用重定向 不加/ 就是以上次访问的路径为准 * 如果加上了/ 就是以项目的跟路径为准 */ @RequestMapping("/addResponse") public ModelAndView addResponse(Student student){ System.out.println("进入了addResponse"); ModelAndView mv=new ModelAndView(); //需要在 webroot下 创建 success.jsp mv.addObject("student", student).setViewName("redirect:/success.jsp"); return mv; } /** * 跳转到内部方法 */ @RequestMapping("/changeMethod") public String changeMethod(Student student){ System.out.println("进入了changeMethod"); ModelAndView mv=new ModelAndView(); mv.addObject("student", student); //return "redirect:list"; 重定向 不能使用 / 跳转到当前controller中的list方法 ! 数据丢失 return "forward:list";// 转发list方法 数据肯定保留 } @RequestMapping("/list") public String list(Student student){ System.out.println("进入了list"); System.out.println(student.getName()); System.out.println(student.getAge()); return "success"; } }
==================接收并返回json数据======================
需要导入jquery和 Gson的jar包
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script type="text/javascript"> $(function(){ $("button").click(function(){ //按钮的点击事件 $.ajax({ url:"user/doAjax", data:{ name:"小黑", age:50 }, success:function(data){ /* 01.在前台转换成json数据 var json=eval("("+data+")"); alert(json.name+ " "+json.age); */ /* 02.在后台设置response.setContentType("application/json"); */ alert(data.name+ " "+data.age); } }) }) }) </script> </head> <body> <button>提交ajax请求</button> </body> </html>