@[toc]javascript
數據回寫:在作數據更新的時候服務端查詢的數據自動填充到表單中。html
controlllerjava
@RequestMapping("/fun") public String fun(String bookName,String author,Integer publicYear,Model model){ if ("xx".equals(bookName)&&"qw".equals(author)&&"2019".equals(publicYear)) { return "/book.jsp"; } model.addAttribute("bookName", bookName); model.addAttribute("author", author); model.addAttribute("publicYear", publicYear); return "forward:/book.jsp"; }
頁面中的回寫web
<form action="add" method="post"> 書名:<input type="text" name="bookName" value="${book.bookName}"><br> 做者:<input type="text" name="author" value="${book.author}"><br> 出版年份<input type="text" name="publicYear" value="${book.publicYear}"><br> <input type="submit" value="提交"> </form>
/** * * @param book * 會自動保存在Model對象中 key是 book * m.addAttribute("book", book); * @return */ @RequestMapping("/update") public String update(Book book) { System.out.println(book); book.setBookName("ps"); return "/book.jsp"; }
@RequestMapping("/add") public String add(@ModelAttribute("bk")Book book) { System.out.println(book); book.setBookName("abc"); return "/book.jsp"; }
<form action="add" method="post"> 書名:<input type="text" name="bookName" value="${bk.bookName}"><br> 做者:<input type="text" name="author" value="${bk.author}"><br> 出版年份<input type="text" name="publicYear" value="${bk.publicYear}"><br> <input type="submit" value="提交"> </form>
@RequestMapping("/add") public String add(@ModelAttribute("bk")Book book) { System.out.println(book); book.setBookName("abc"); return "/book.jsp"; } /** * 每一個方法都會方法的數據 * * @return */ @ModelAttribute("list") public List<String> getList() { return Arrays.asList("aa", "bb", "cc"); }
${list}<br> <form action="add" method="post"> 書名:<input type="text" name="bookName" value="${bk.bookName}"><br> 做者:<input type="text" name="author" value="${bk.author}"><br> 出版年份<input type="text" name="publicYear" value="${bk.publicYear}"><br> <input type="submit" value="提交"> </form>
項目中的異常須要統一處理,正常狀況下,須要提早準備好一個錯誤頁面,當項目出錯了,將該頁面展現給用戶。 步驟:ajax
package com.sxt.resolver; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerExceptionResolver; import org.springframework.web.servlet.ModelAndView; /** * 全局異常處理器 * @author Administrator * */ @Component public class GlobalExecptionResolver implements HandlerExceptionResolver { /** * 全局異常處理的方法 * return 返回null不執行,按照沒有攔截的方式處理 */ @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,Object handler, Exception ex) { System.out.println("異常發生了"); ModelAndView m=new ModelAndView(); m.setViewName("/error.jsp"); return m; } }
jar包 spring
@Controller public class SpringController { @RequestMapping("/fun1") @ResponseBody public Book fun1(){ Book b=new Book(); b.setBookId(1001); b.setBookName("ps"); b.setAuthor("sp"); return b; } @RequestMapping("/fun2") @ResponseBody public String[] fun2(){ return new String[]{"a","b","c"}; } @RequestMapping("/fun3") @ResponseBody public List<String> fun3(){ return Arrays.asList("bb","aa","cc"); } @RequestMapping("/fun4") @ResponseBody public Map<String, Object> fun4(){ Map<String, Object>map=new HashMap<>(); Book b=new Book(); b.setBookId(1001); b.setBookName("ps"); b.setAuthor("sp"); map.put("b1", b); Book b2=new Book(); b2.setBookId(1001); b2.setBookName("ps"); b2.setAuthor("sp"); map.put("b2", b2); map.put("total", 2); return map; } }
導入juery文件json
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="js/juery.min.js"></script> </head> <body> <input type="button" value="提交JSON數據" onclick="fun1();"> <script type="text/javascript"> function fun1() { $.ajax({ type : 'POST', url : "test2", contentType : "application/json",//若是想以json格式把數據提交到後臺的話,這個必須有,不然只會當作表單提交 data : JSON.stringify({ "bookId" : "2019", "bookName" : "ps" }),//JSON.stringify()必須有,不然只會當作表單的格式提交 dataType : "json",//期待返回的數據類型 success : function(data) { alert("success:" + data); }, error : function(data) { alert("error" + data); } }); } </script> </body> </html>
<!-- 開啓掃描 --> <context:component-scan base-package="com.sxt.*"/> <!-- 開啓SpringMVC註解的方式 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"></bean> </mvc:message-converters> </mvc:annotation-driven>
在web.xml文件中配置mvc
<!-- spring框架提供的字符集過濾器 --> <!-- spring Web MVC框架提供了org.springframework.web.filter.CharacterEncodingFilter用於解決POST方式形成的中文亂碼問題 --> <filter> <filter-name>encodingFilter</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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter>