jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="js/jquery.min.js"></script>
</head>
<body>
<script>
/*页面加载完成*/
// $.post() 将json串 转成 JavaBean对象不成功 用原生的可以 如下下
/* $(function () {
//绑定单击事件!
$("#butt").click(function () {
alert("$.post()方式...");
$.post("account/ajaxDemo", {id: "29", name: "jsonoo", money: "600d"}, function (data) {
alert(data.name);
alert(data.money);
},"json");
});
});*/
$(function () {
$("#butt").click(function () {
alert("原生ajax...")
$.ajax({
url: "account/ajaxDemo",
contentType: "application/json;charset=UTF-8",
data: '{"id":"20","name":"javaa","money":"9000"}',
dataType: "json",
type: "post",
success: function (data) {
alert(data.name);
alert(data.money);
}
});
});
});
</script>
<button id="butt" type="button">Ajax请求</button><br>
<a href="account/findAll">测试查询</a><br>
<form action="account/saveAccount" method="post">
姓名:<input type="text" name="name"><br>
金额:<input type="text" name="money"><br>
<input type="submit" value="保存">
</form>
<form action="account/transfer" method="post">
姓名:<input type="text" name="oldAccount"><br>
姓名:<input type="text" name="newAccount"><br>
金额:<input type="text" name="money"><br>
<input type="submit" value="保存">
</form>
<a href="account/initUpdat">initUpdate默认页面</a><br>
<a href="account/textDemo">请求转发</a><br>
<a href="account/textDemo1">请求转发1</a><br>
<a href="account/springForward">SpringMVC_请求转发</a><br>
<a href="account/textDemo01">重定向01</a><br>
<a href="account/textDemo02">重定向02</a><br>
<a href="account/springRedirect">SpringMVC_重定向</a><br>
<a href="account/writeDemo">直接写出</a><br>
<a href="account/findAll01">MedolAndView</a><br>
</body>
</html>
controller(处理器)
package com.itcast.controller;
import com.itcast.domain.Account;
import com.itcast.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private AccountService accountService;
/**
* @param account
* @return
* @RequestBody: 1.获取请求体 2.json的字符串转换成JavaBean的对象
* @ResponseBody 把JavaBean对象转换成json字符串,直接响应 要求方法需要返回JavaBean的对象
*/
@RequestMapping(value = "/ajaxDemo")
public @ResponseBody Account ajaxDemo(@RequestBody Account account) {
System.out.println("ajaxDemo");
System.out.println(account);
return account;
}
//@RequestBody获取请求体
/* @RequestMapping("/ajaxDemo")
public void ajaxDemo(@RequestBody String body){
System.out.println(body);//name=json&money=600
}*/
@RequestMapping("/findAll")
public String findAll(Model model) {
System.out.println("表现层:查询所有账号。。。");
List<Account> list = accountService.findAll();
//request 域对象 一样的 功能 前端页面 同样可以用el表达式
model.addAttribute("listt", list);
for (Account account : list) {
System.out.println(account);
}
return "list";
}
@RequestMapping("/saveAccount")
public String saveAccount(Account account) {
System.out.println("表现层:保存用户执行了。。。");
accountService.saveAccount(account);
System.out.println(account.getName() + "的账户有:" + account.getMoney());
return "list";
}
@RequestMapping("/transfer")
public String transfer(String oldAccount, String newAccount, Double money) {
System.out.println("表现层:转账执行了。。。");
accountService.transfer(oldAccount, newAccount, money);
System.out.println(oldAccount + "给" + newAccount + "转账了" + money);
return "list";
}
//没有返回值 默认跳转 跳转到account/initUpdat.jsp 页面
@RequestMapping("/initUpdat")
public void initUpdate() {
System.out.println("initUpdate默认页面执行...");
}
@RequestMapping("/textDemo")
public void textDemo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/pages/list.jsp").forward(request, response);
System.out.println("textDemo--请求转发--执行....");
}
@RequestMapping("/springForward") //SpringMVC提供的请求转发(返回String类型)
public String springForward(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("springForward--请求转发--执行....");
return "forward:/WEB-INF/pages/list.jsp";
//不走视图解析器了,所以需要编写完整的路径
// return "forward:/aaa.jsp";
}
@RequestMapping("/textDemo1")
public void textDemo1(HttpServletRequest request, HttpServletResponse response) throws Exception {
request.getRequestDispatcher("/aaa.jsp").forward(request, response);
System.out.println("textDemo1--请求转发--执行....");
}
//重定向到jsp页面,则jsp页面不能写在 WEB-INF 目录中,否则无法找到
@RequestMapping("/textDemo01")
public void textDemo01(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.sendRedirect(request.getContextPath() + "/WEB-INF/pages/list.jsp");//错误
System.out.println("textDemo01--重定向--执行....");
}
@RequestMapping("/springRedirect") //SpringMVC提供的重定向(返回String类型)
public String springRedirect(HttpServletRequest request, HttpServletResponse response) {
System.out.println("textDemo02--重定向--执行....");
return "redirect:/aaa.jsp";//正确
//return "redirect:"+request.getContextPath()+"/aaa.jsp";错误写法,不用加虚拟路径(工程名)框架底层 帮忙加了
}
@RequestMapping("/textDemo02")
public void textDemo02(HttpServletRequest request, HttpServletResponse response) throws Exception {
response.sendRedirect(request.getContextPath() + "/aaa.jsp");
System.out.println("textDemo02--重定向--执行....");
}
@RequestMapping("/writeDemo")
public void writeDemo(HttpServletResponse response) throws Exception {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().print("您好!!!");
}
//返回值ModelAndView
@RequestMapping("/findAll01")
public ModelAndView findAll01() {
ModelAndView mv = new ModelAndView();
//返回视图
mv.setViewName("findAll");
List<Account> list = accountService.findAll();
System.out.println(list);
//返回数据
mv.addObject("aaa", list);
return mv;
}
}
json字符串和JavaBean对象互相转换的过程中,需要使用jackson的jar包
<!-- json字符串和JavaBean对象互相转换的过程中,需要使用jackson的jar包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.0</version>
</dependency>
ssm整合 web.xml 配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<!--配置前端控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern> <!--拦截所有资源 需要在springmvc.xml设置静态资源放行-->
</servlet-mapping>
<!--配置解决中文乱码 的 过滤器-->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置监听器 整合spring 加载applicationContext.xml配置文件-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载类路径的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>