不響應 | void+@ResponseBody註解 |html
ModelAndView | 經過setViewName方法 | 直接指定響應頁面 | 返回值爲String類型,返回結果指定跳轉地址 |java
重定向 | 跳轉地址前加redirect:前綴便可 |web
HttpServletRequest和HttpServletResponse | 形參中聲明這兩個變量。而後經過相關api跳轉|spring
返回值爲void時,方法中能夠不用作任何返回,在瀏覽器中,springmvc會默認去查找和方法同名的頁面做爲方法的視圖返回。 若是確實不須要該方法返回頁面,可使用@ResponseBody註解,表示一個請求到此爲止。api
@RequestMapping("/test1") @ResponseBody public void test1() { System.out.println("test1"); }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SpringMVC-01-hello</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <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:Spring-MVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- 開啓掃描 --> <context:component-scan base-package="com.sxt"/> <!-- 開啓SpringMVC註解的方式 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 配置視圖解析器 和Controller的一個方法一塊兒使用後面有標記--> <!-- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/"/> <property name="suffix" value=".jsp"/> </bean> --> </beans>
package com.sxt; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/hello") public class HelloController { //ModelAndView,響應方式 @RequestMapping("/hello1") public ModelAndView hello1(){ System.out.println("-----hello1---"); ModelAndView m = new ModelAndView(); m.setViewName("/index.jsp"); return m; } //返回一個字符串 @RequestMapping("/hello2") @ResponseBody//該註解表示一個請求到此結束 public void hello2(){ System.out.println("aaaa"); } /* * 此方法須要在spring-mvc的配置文件配置視圖解析器,自動添加index的前綴後綴,開啓後可用這個方法 @RequestMapping("/h1") public String hello3(){ System.out.println("111"); return "index"; }*/ //重定向跳轉 :返回路徑注意: 返回的字符帶"/「表示從根目錄下開始找,不帶」/"從當前目錄下查找 @RequestMapping("/h2") public String hello4(){ System.out.println("111"); return "redirect:/index.jsp"; } //經過request和response @RequestMapping("/h5") public void hello5(HttpServletRequest request,HttpServletResponse response) throws IOException, Exception{ System.out.println("333"); request.getRequestDispatcher("/index.jsp").forward(request, response); } }
上面方法除了視圖解析器須要在配置文件配置,其他的都同樣數組
1.映射路徑 @RequestMapping最基本的功能,用法:瀏覽器
@RequestMapping("/delete") public String delete(){ System.out.println("波波烤鴨:刪除數據操做...."); return "/hello"; }
窄化請求 窄化請求用來限定請求路徑,即將@RequestMapping放在類上,這樣,方法的請求路徑是類上的@ReqmestMapping+方法上的@RequestMapping 請求方法限定 spring-mvc
java基本數據類型+string 使用基本數據類型時,參數名稱和瀏覽器傳來的參數的key一致,這樣才能實現自動映射session
/** * 接收參數 * 基本數據類型 * @param id * @param name * @return */ @RequestMapping("add") public String add(int id,String name){ System.out.println(id+"---"+name); return "/hello"; }
若是參數名和瀏覽器傳來的key不一致,能夠經過@RequestParam來解決。以下mvc
/** * 接收參數 * 基本數據類型 * 請求參數若是和形參名稱不一致能夠經過@RequestParam類指定 * @param id * @param name * @return */ @RequestMapping("add") public String add(int id,@RequestParam("username")String name){ System.out.println(id+"---"+name); return "/hello"; }
加@ReuestParam,若是爲從新指定參數名,則默認的參數名依然是本來的參數名,同時也要注意,添加了這個註解後,對應的參數將成爲必填參數.若是沒有傳遞相關的參數,則會拋異常 但若是不想傳參數,也有兩種方式解決
/** * 接收參數 * 基本數據類型 * 請求參數若是和形參名稱不一致能夠經過@RequestParam類指定 * @param id * @param name * @return */ @RequestMapping("add") public String add(int id ,@RequestParam(value="username",required=false)String name){ System.out.println(id+"---"+name); return "/hello"; }
/** * 接收參數 * 基本數據類型 * 請求參數若是和形參名稱不一致能夠經過@RequestParam類指定 * @param id * @param name * @return */ @RequestMapping("add") public String add(int id ,@RequestParam(value="username",defaultValue="kaoya")String name){ System.out.println(id+"---"+name); return "/hello"; }
package com.sxt.bean; public class Book { private Integer id; private String name; @Override public String toString() { return "Book [id=" + id + ", name=" + name + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.sxt.bean; import java.util.Arrays; import java.util.Date; import java.util.List; public class User { private Integer id; private Integer age; private String unama; private String[] favorites; private List<String> list; private Date birth; private Book book; @Override public String toString() { return "User [id=" + id + ", age=" + age + ", unama=" + unama + ", favorites=" + Arrays.toString(favorites) + ", list=" + list + ", birth=" + birth + ", book=" + book + "]"; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getUnama() { return unama; } public void setUnama(String unama) { this.unama = unama; } public String[] getFavorites() { return favorites; } public void setFavorites(String[] favorites) { this.favorites = favorites; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public Book getBook() { return book; } public void setBook(Book book) { this.book = book; } }
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- 開啓掃描 --> <context:component-scan base-package="com.sxt"/> <!-- 開啓SpringMVC註解的方式 --> <mvc:annotation-driven></mvc:annotation-driven> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="add" method="post"> <table> <tr> <td>編號</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名字</td> <td><input type="text" name="unama"></td> </tr> <tr> <td>年齡</td> <td><input type="text" name="age"></td> </tr> <tr> <td>id</td> <td><input type="text" name="book.id"></td> </tr> <tr> <td>做者</td> <td><input type="text" name="book.name"></td> </tr> <tr> <td><input type="submit" value="添加"></td> </tr> </table> </form> </body> </html>
package com.sxt; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.sxt.bean.User; @Controller public class UserController { @RequestMapping("/add")//對象接受數據方法 @ResponseBody public void add(User user) { System.out.println(user); } }
由於測試的對象因此其餘都沒給值
bean層和配置文件,web.xml文件都同樣
package com.sxt; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.sxt.bean.User; @Controller public class UserController { //數組數據獲取 @RequestMapping("/add2") @ResponseBody public void add3(User user) { System.out.println(user); String[] favorites = user.getFavorites(); for (String f : favorites) { System.out.println(f); } } //集合數據獲取 @RequestMapping("/add3") @ResponseBody public void add4(User user) { System.out.println(user); System.out.println(user.getList()); } }
獲取數組類的jsp頁面 獲取集合類的jsp頁面
總結: 1.數組(不管是基本數據類型仍是對象數組)均可以直接寫在接口參數中。 2.集合(不管是基本數據類型仍是對象)都須要一個包裝類將其包裝起來,不能直接寫在接口參數中。 3.對於基本數據類型,數組和集合在表單中的寫法是同樣的 4.對於對象數據類型,數組和集合在表單中的寫法是同樣的
接受數據類型是Data類型的須要經過轉換器進行接受
package com.sxt; import java.util.Date; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.sxt.bean.User; @Controller public class UserController { @RequestMapping("/add4") @ResponseBody public void add4(Date d) { System.out.println(d); } }
package com.sxt; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.core.convert.converter.Converter; /** * Data類型轉換器 * @author Administrator * */ public class Convert implements Converter<String, Date>{ @Override public Date convert(String arg0) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); try { return format.parse(arg0); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- 開啓掃描 --> <context:component-scan base-package="com.sxt"/> <!-- 開啓SpringMVC註解的方式 --> <mvc:annotation-driven conversion-service="formattingConversionServiceFactoryBean"></mvc:annotation-driven> <bean class="org.springframework.format.support.FormattingConversionServiceFactoryBean" id="formattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.sxt.Convert"/> </set> </property> </bean> </beans>
3.1ModelAndView 3.2HttpServletRequest 3.3HttpSession 3.4Map
package com.sxt; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.SessionAttributes; import org.springframework.web.servlet.ModelAndView; @Controller public class HelloController { @RequestMapping("query1") public String add(Map<String, Object> map){ map.put("msg", "aaaa"); return "/index.jsp"; } @RequestMapping("query2") public String add1(Model m){ m.addAttribute("msg", "bbb"); return "/index.jsp"; } @RequestMapping("query3") public String add2(ModelMap mm){ mm.addAttribute("msg", "ccc"); return "/index.jsp"; } @RequestMapping("query4") public ModelAndView add3(){ ModelAndView view = new ModelAndView(); view.addObject("msg", "ddd"); view.setViewName("index.jsp"); return view; } }
<?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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- 開啓掃描 --> <context:component-scan base-package="com.sxt"/> <!-- 開啓SpringMVC註解的方式 --> <mvc:annotation-driven ></mvc:annotation-driven> </beans>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> ${msg} <h3>request:${requestScope.msg }</h3> <h3>session:${sessionScope.msg }</h3> <h3>application:${applicationScope.msg }</h3> </body> </html>
request:${requestScope.msg } session:${sessionScope.msg } application:${applicationScope.msg } 檢查msg是保存在哪個做用域的,結果證實都是保存在request做用域的
注意:加上@SessionAttributes這個註解是將數據保存在session做用域中的.
<!-- 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> </filter> <filter-mapping > <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>