第一章: 回顧JavaWeb中的MVC設(shè)計(jì)模式
- MVC這種設(shè)計(jì)模式,不光運(yùn)用于Web領(lǐng)域蛇数,而且也能用于非Web領(lǐng)域
- 這里說的MVC特指一種表現(xiàn)層設(shè)計(jì)模式挪钓,不限于Java語言
第二章: 回顧struts2+Spring開發(fā)
- 需求:學(xué)生注冊【add.jsp->StudentAction.java->addOK.jsp】
- struts2框架有如下特點(diǎn):
- 每次請求action時(shí),都創(chuàng)建action實(shí)例
- action類一成不變的直接或間接繼續(xù)ActionSupport類
- action類中的業(yè)務(wù)控制方法總是相類似的簽名且無參
- action類中耳舅,接收參數(shù)要用實(shí)例變量和對(duì)應(yīng)的set方法或set/get方法struts.xml配置文件碌上,必須以struts.xml命名倚评,且放在src目錄下
第三章: 什么是springmvc,它與spring有什么關(guān)系
- springmvc屬于spring框架的后續(xù)產(chǎn)品馏予,用在基于MVC的表現(xiàn)層開發(fā)天梧,類似于struts2框架
第四章: 初識(shí)springmvc工作流程
第五章: springmvc快速入門(XML版本)
- springmvc快速入門(傳統(tǒng)版)
1> 創(chuàng)建springmvc-day01這么一個(gè)web應(yīng)用
2> 導(dǎo)入springioc,springweb , springmvc相關(guān)的jar包
3> 在/WEB-INF/下創(chuàng)建web.xml文件
<!-- 注冊springmvc框架核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
4> 創(chuàng)建HelloAction.java控制器類
/**
* 控制器
* @author AdminTC
*/
public class HelloAction implements Controller{
/**
* 業(yè)務(wù)方法
*/
public ModelAndView handleRequest(HttpServletRequest requqest,HttpServletResponse response) throws Exception {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message","這是我的第一個(gè)springmvc應(yīng)用程序");
modelAndView.setViewName("/jsp/success.jsp");
return modelAndView;
}
}
5> 在/WebRoot/下創(chuàng)建jsp/success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>這是我的第一個(gè)springmvc應(yīng)用程序</title>
</head>
<body>
success.jsp<br/>
${message}
</body>
</html>
6> 在/WEB-INF/創(chuàng)建DispatcherServlet-servlet.xml配置文件霞丧,xml頭部信息與spring.xml相同(該配置文件的命名規(guī)則為: web.xml文件中配置的<servlet-name>的值-servlet.xml)
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 控制器(程序員) -->
<bean name="/hello.action" class="cn.itcast.javaee.springmvc.base.HelloAction"></bean>
<!-- 映射器(框架) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 適配器(框架) -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 視圖解析器(框架) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</beans>
7> 部署web應(yīng)用到tomcat中呢岗,通過瀏覽器訪問如下URL:http://127.0.0.1:8080/springmvc-day01/hello.action
第六章 加載自定義目錄下的springmvc.xml配置文件
- 在默認(rèn)情況下:springmvc框架的配置文件必須叫<servlet-name>-servlet.xml
且必須放在/WEB-INF/目錄下,我們可以在web.xml文件中蛹尝,為DispatcherServlet配置一個(gè)初始化參數(shù)后豫,讓它去我們指定的目錄下加載springmvc.xml配置文件,web.xml代碼如下:
<!-- 注冊springmvc框架核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/cn/itcast/javaee/springmvc/config/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
- 如果springmvc.xml配置文件放在src目錄下突那,web.xml代碼如下:
<!-- 注冊springmvc框架核心控制器 -->
<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:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
第七章: 視圖解析器InternalResourceViewResolver【解析視圖邏輯名對(duì)應(yīng)的真實(shí)路徑】
- ModelAndView對(duì)象中即可以封裝真實(shí)視圖路徑名挫酿,也可以封裝視圖路徑的邏輯名,springmvc.xml代碼如下:
<!-- 視圖解析器(框架) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
- Action類代碼如下:
modelAndView.setViewName("success");
第八章: 映射器Mapping【什么樣的請求交給Action】
- 將自定義的Action所對(duì)應(yīng)的<bean>標(biāo)簽的name屬性作為請求路徑
:org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping(核心)
<!-- 注冊控制器(程序員) -->
<bean name="/add.action" class="cn.itcast.javaee.springmvc.mapping.UserAction"></bean>
<!-- 注冊映射器(handler包)(框架) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
-
org.springframework.web.servlet.handler.SimpleUrlHandlerMapping
: /delete.action和/update.action和/find.action請求路徑都交由<bean>標(biāo)簽為id的Action愕难,即多個(gè)路徑對(duì)應(yīng)同一個(gè)Action
<!-- 注冊控制器(程序員) -->
<bean id="userActionID" class="cn.itcast.javaee.springmvc.mapping.UserAction"></bean>
<!-- 注冊映射器(handler包)(框架) -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/delete.action">userActionID</prop>
<prop key="/update.action">userActionID</prop>
<prop key="/find.action">userActionID</prop>
</props>
</property>
</bean>
第九章: 適配器Adapter【Action實(shí)現(xiàn)什么接口】
- Action實(shí)現(xiàn)Controller接口
:org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter
public class EmpAction implements Controller{
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception {
System.out.println("EmpAction::handleRequest");
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message",username);
modelAndView.setViewName("/jsp/success.jsp");
return modelAndView;
}
}
第十章: 控制器Controller【Action繼承什么類】
-
org.springframework.web.servlet.mvc.ParameterizableViewController
: 如果請求是/hello.action的請求路徑早龟,則直接跳轉(zhuǎn)到/jsp/success.jsp頁面,不經(jīng)過程序員定義的控制器Action
<!-- /index.action請求猫缭,直接轉(zhuǎn)發(fā)到/index.jsp頁面 -->
<bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/index.jsp"/>
</bean>
<!-- 注冊視圖解析器(view包)(框架)
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
-->
-
org.springframework.web.servlet.mvc.AbstractCommandController
: 能夠以實(shí)體的形式葱弟,收集客戶端參數(shù)- Action類:
public class AdminAction extends AbstractCommandController{
public AdminAction(){
this.setCommandClass(Admin.class);
}
@Override
protected ModelAndView handle(HttpServletRequest request,HttpServletResponse response, Object obj, BindException bindException)throws Exception {
System.out.println("AdminAction::handle");
ModelAndView modelAndView = new ModelAndView();
Admin admin = null;
if(obj instanceof Admin){
admin = (Admin) obj;
}
modelAndView.addObject("username",admin.getUsername());
modelAndView.addObject("gender",admin.getGender());
modelAndView.addObject("hiredate",admin.getHiredate());
modelAndView.setViewName("/jsp/success.jsp");
return modelAndView;
}
}
- springMVC配置文件:
<!-- 請求處理類 -->
<bean name="/add.action" class="cn.itcast.javaee.springmvc.controller.AdminAction">
</bean>
<!-- 映射器 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
</bean>
第十一章: 日期轉(zhuǎn)換器和編碼過濾器
- 在默認(rèn)情況下,springmvc不能將String類型轉(zhuǎn)成Date類型猜丹,必須自定義類型轉(zhuǎn)換器
public class AdminAction extends AbstractCommandController{
@Override
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
- spring提供的芝加,專用于解決POST提交中文亂碼問題,需要在web.xml文件中配置
<!-- 編碼過濾器 -->
<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>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第十二章: springmvc與struts2的區(qū)別
1> 入口 :
- springmvc的入口是一個(gè)servlet居触,即前端控制器妖混,例如:*.action
- struts2入口是一個(gè)filter過慮器,即前端過濾器轮洋,例如:/*
2> 基于的開發(fā)方式 :
- springmvc是基于方法開發(fā)制市,傳遞參數(shù)是通過方法形參,可以設(shè)計(jì)為單例 --
- struts2是基于類開發(fā)弊予,傳遞參數(shù)是通過類的屬性祥楣,只能設(shè)計(jì)為多例
3> 工作流程 :
- springmvc通過參數(shù)解析器是將request對(duì)象內(nèi)容進(jìn)行解析成方法形參,將響應(yīng)數(shù)據(jù)和頁面封裝成
- ModelAndView對(duì)象汉柒,最后又將模型數(shù)據(jù)通過request對(duì)象傳輸?shù)巾撁?br> struts采用值棧存儲(chǔ)請求和響應(yīng)的數(shù)據(jù)误褪,通過OGNL存取數(shù)據(jù)
第十四章: springmvc快速入門(注解版本)
1)springmvc快速入門(注解版)
1> 創(chuàng)建springmvc-day02這么一個(gè)web應(yīng)用
2> 導(dǎo)入springioc,springweb和springmvc相關(guān)的jar包
------------------------------------------------------springWEB模塊
org.springframework.web-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar(mvc專用)
------------------------------------------------------springIOC模塊
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
3> 在/WEB-INF/下創(chuàng)建web.xml文件
<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:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
4> 創(chuàng)建HelloAction.java控制器類
@Controller
public class HelloAction{
@RequestMapping(value="/hello")
public String helloMethod(Model model) throws Exception{
System.out.println("HelloAction::helloMethod()");
model.addAttribute("message","這是我的第二個(gè)springmvc應(yīng)用程序");
return "/success.jsp";
}
}
5> 在/WebRoot/下創(chuàng)建success.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>這是我的第二個(gè)springmvc應(yīng)用程序</title>
</head>
<body>
success.jsp<br/>
${message}
</body>
</html>
6> 在/src/目錄下創(chuàng)建spring.xml配置文件
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<!-- Action控制器 -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.helloannotation"/>
<!-- 基于注解的映射器(可選) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- 基于注解的適配器(可選) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 視圖解析器(可選) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
</beans>
7> 部署web應(yīng)用到tomcat中碾褂,通過瀏覽器訪問如下URL : http://127.0.0.1:8080/springmvc/hello.action
第十五章 一個(gè)Action中兽间,可以寫多個(gè)類似的業(yè)務(wù)控制方法
1> 通過模塊根路徑 + 功能子路徑 = 訪問模塊下子功能的路徑
@Controller
@RequestMapping(value="/user")
public class UserAction{
@RequestMapping(value="/add")
public String add(Model model) throws Exception{
System.out.println("HelloAction::add()");
model.addAttribute("message","增加用戶");
return "/success.jsp";
}
@RequestMapping(value="/find")
public String find(Model model) throws Exception{
System.out.println("HelloAction::find()");
model.addAttribute("message","查詢用戶");
return "/success.jsp";
}
}
- 增加用戶:http://127.0.0.1:8080/myspringmvc-day02/user/add.action
- 查詢用戶:http://127.0.0.1:8080/myspringmvc-day02/user/find.action
第十六章 在業(yè)務(wù)控制方法中寫入普通變量收集參數(shù)
1> 可以在業(yè)務(wù)控制方法中,以參數(shù)形式收集客戶端參數(shù)正塌,springmvc采用方法參數(shù)形式的
@Controller
@RequestMapping(value="/user")
public class UserAction{
@RequestMapping(value="/add")
public String add(Model model,int id,String name,Double sal) throws Exception{
System.out.println("HelloAction::add()");
System.out.println(id + ":" + name + ":" + sal);
model.addAttribute("message","增加用戶");
return "/success.jsp";
}
}
http://127.0.0.1:8080/myspringmvc-day02/user/add.action?id=1&name=zhaojun&sal=5000
第十七章 限定某個(gè)業(yè)務(wù)控制方法嘀略,只允許GET或POST請求方式訪問
1)可以在業(yè)務(wù)控制方法前恤溶,指明該業(yè)務(wù)控制方法只能接收GET或POST的請求
@Controller
@RequestMapping(value="/user")
public class UserAction{
@RequestMapping(value="/add",method=RequestMethod.POST)
public String add(Model model,int id,String name,double sal) throws Exception{
System.out.println("HelloAction::add()::POST");
System.out.println(id + ":" + name + ":" + sal);
model.addAttribute("message","增加用戶");
return "/success.jsp";
}
}
如果不書寫method=RequestMethod.POST的話,GET和POST請求都支持
第十八章 在業(yè)務(wù)控制方法中寫入Request帜羊,Response等傳統(tǒng)web參數(shù)
1)可以在業(yè)務(wù)控制方法中書寫傳統(tǒng)web參數(shù)咒程,這種方式我們不提倡,耦合了
@Controller
@RequestMapping(value="/user")
public class UserAction{
@RequestMapping(value="/add",method=RequestMethod.POST)
public void add(HttpServletRequest request,HttpServletResponse response) throws Exception{
System.out.println("HelloAction::add()::POST");
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
double sal = Double.parseDouble(request.getParameter("sal"));
System.out.println(id + ":" + name + ":" + sal);
request.getSession().setAttribute("id",id);
request.getSession().setAttribute("name",name);
request.getSession().setAttribute("sal",sal);
response.sendRedirect(request.getContextPath()+"/register.jsp");
}
}
第十九章 在業(yè)務(wù)控制方法中寫入模型變量收集參數(shù)讼育,且使用@InitBind來解決字符串轉(zhuǎn)日期類型
1> 在默認(rèn)情況下帐姻,springmvc不能將String類型轉(zhuǎn)成java.util.Date類型,所有我們只能在Action中自定義類型轉(zhuǎn)換器
<form action="${pageContext.request.contextPath}/user/add.action" method="POST">
編號(hào):<input type="text" name="id" value="${id}"/><br/>
姓名:<input type="text" name="name" value="${name}"/><br/>
薪水:<input type="text" name="sal" value="${sal}"/><br/>
入職時(shí)間:<input type="text" name="hiredate" value='<fmt:formatDate value="${hiredate}" type="date"/>'/><br/>
<input type="submit" value="注冊"/>
</form>
@Controller
@RequestMapping(value = "/user")
public class UserAction {
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(int id, String name, double sal, Date hiredate,
Model model) throws Exception {
System.out.println("HelloAction::add()::POST");
model.addAttribute("id", id);
model.addAttribute("name", name);
model.addAttribute("sal", sal);
model.addAttribute("hiredate", hiredate);
return "/register.jsp";
}
}
第二十章 在業(yè)務(wù)控制方法中寫入U(xiǎn)ser奶段,Admin多個(gè)模型收集參數(shù)
1> 可以在業(yè)務(wù)控制方法中書寫1個(gè)模型來收集客戶端的參數(shù)
2> 模型中的屬性名必須和客戶端參數(shù)名一一對(duì)應(yīng)
3> 這里說的模型不是Model對(duì)象饥瓷,Model是向視圖中封裝的數(shù)據(jù)
@Controller
@RequestMapping(value = "/user")
public class UserAction {
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(User user,Model model) throws Exception {
System.out.println("HelloAction::add()::POST");
model.addAttribute("user",user);
return "/register.jsp";
}
}
第二十一章 在業(yè)務(wù)控制方法中寫入包裝User的模型來收集參數(shù)
-
可以在業(yè)務(wù)控制方法中書寫0個(gè)或多個(gè)模型來收集客戶端的參數(shù)
1> 如果多個(gè)模型中有相同的屬性時(shí),可以用user.name或admin.name來收集客戶端參數(shù)
2> 用一個(gè)新的模型將User和Admin再封裝一次- User.java
public class User {
private Integer id;
private String name;
private Double sal;
private Date hiredate;
public User(){}
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;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return this.id + ":" + this.name + ":" + this.sal + ":" + this.hiredate;
}
}
- Bean.java
public class Bean {
private User user;
private Admin admin;
public Bean(){}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Admin getAdmin() {
return admin;
}
public void setAdmin(Admin admin) {
this.admin = admin;
}
}
- PersonAction.java
@Controller
@RequestMapping(value = "/person")
public class PersonAction {
@InitBinder
protected void initBinder(HttpServletRequest request,ServletRequestDataBinder binder) throws Exception {
binder.registerCustomEditor(
Date.class,
new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String add(Bean bean,Model model) throws Exception {
System.out.println(bean.getUser());
System.out.println(bean.getAdmin());
System.out.println("PersonAction::add()::POST");
model.addAttribute("bean",bean);
return "/register.jsp";
}
}
- register.jsp
普通用戶
<form action="${pageContext.request.contextPath}/person/add.action" method="POST">
編號(hào):<input type="text" name="user.id" value="${bean.user.id}"/><br/>
姓名:<input type="text" name="user.name" value="${bean.user.name}"/><br/>
薪水:<input type="text" name="user.sal" value="${bean.user.sal}"/><br/>
入職時(shí)間:<input type="text" name="user.hiredate" value='<fmt:formatDate value="${bean.user.hiredate}" type="both" />'/><br/>
<input type="submit" value="注冊"/>
</form>
第二十二章 在業(yè)務(wù)控制方法中收集數(shù)組參數(shù)
- 批量刪除用戶
@Controller
@RequestMapping(value="/user")
public class UserAction {
@RequestMapping(value="/delete")
public String deleteMethod(int[] ids,Model model) throws Exception{
System.out.println("UserAction::deleteMethod()");
System.out.println("需要?jiǎng)h除的id為:");
for(int id : ids){
System.out.print(id+" ");
}
model.addAttribute("message","批量刪除成功");
return "/success.jsp";
}
}
第二十三章 在業(yè)務(wù)控制方法中收集List<JavaBean>參數(shù)
- 批量注冊用戶
- UserAction.java
@Controller
@RequestMapping(value="/user")
public class UserAction {
@RequestMapping(value="/addAll")
public String addAll(Bean bean,Model model) throws Exception{
for(User user : bean.getUserList()){
System.out.println(user.getName()+":"+user.getGender());
}
model.addAttribute("message","批量增加用戶成功");
return "/success.jsp";
}
}
- Bean.java
public class Bean {
private List<User> userList = new ArrayList<User>();
public Bean(){}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
- registerAll.java
<form action="${pageContext.request.contextPath}/user/addAll.action" method="POST">
姓名:<input type="text" name="userList[0].name" value="哈哈"/>
性別:<input type="text" name="userList[0].gender" value="男"/>
<hr/>
姓名:<input type="text" name="userList[1].name" value="呵呵"/>
性別:<input type="text" name="userList[1].gender" value="男"/>
<hr/>
姓名:<input type="text" name="userList[2].name" value="嘻嘻"/>
性別:<input type="text" name="userList[2].gender" value="女"/>
<hr/>
<input type="submit" value="批量注冊"/>
</form>
第二十四章 結(jié)果的轉(zhuǎn)發(fā)和重定向
1> 在轉(zhuǎn)發(fā)情況下痹籍,共享request域?qū)ο罂肝椋瑫?huì)將參數(shù)從第一個(gè)業(yè)務(wù)控制方法傳入第二個(gè)業(yè)務(wù)控制方法,反之词裤,重定向則不行, 刪除id=10號(hào)的用戶,再查詢用戶
@Controller
@RequestMapping(value="/user")
public class UserAction {
@RequestMapping(value="/delete")
public String delete(int id) throws Exception{
System.out.println("刪除用戶->" + id);
//轉(zhuǎn)發(fā)到find()
return "forward:/user/find.action";
//重定向到find()
//return "redirect:/user/find.action";
}
@RequestMapping(value="/find")
public String find(int id) throws Exception{
System.out.println("查詢用戶->" + id);
return "/success.jsp";
}
}
第二十五章 異步發(fā)送表單數(shù)據(jù)到JavaBean鳖宾,并響應(yīng)JSON文本返回
1> 提交表單后吼砂,將JavaBean信息以JSON文本形式返回到瀏覽器
- bean2json.jsp
<form>
編號(hào):<input type="text" name="id" value="1"/><br/>
姓名:<input type="text" name="name" value="哈哈"/><br/>
薪水:<input type="text" name="sal" value="5000"/><br/>
<input type="button" value="異步提交注冊"/>
</form>
<script type="text/javascript">
$(":button").click(function(){
var url = "${pageContext.request.contextPath}/user/add.action";
var sendData = {
"id":1,
"name":"哈哈",
"sal":5000
};
$.post(url,sendData,function(backData,textStatus,ajax){
alert(ajax.responseText);
});
});
</script>
- User.java
public class User {
private Integer id;
private String name;
private Double sal;
public User(){}
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;
}
public Double getSal() {
return sal;
}
public void setSal(Double sal) {
this.sal = sal;
}
}
- UserAction.java
@Controller
@RequestMapping(value="/user")
public class UserAction {
@RequestMapping(value="/add")
public @ResponseBody User add(User user) throws Exception{
System.out.println(user.getId()+":"+user.getName()+":"+user.getSal());
return user;
}
}
- spring.xml
<!-- Action控制器 -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.app25"/>
<!-- 配適器 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>