Springmvc的jar包下载:https://pan.baidu.com/s/1Uu5R96z4LwwtydGq4B60Xg 密码:8c3n
ItemController.java(Controller处理器,传统Servlet方式接收参数):
package com.xxx.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.xxx.springmvc.pojo.Items;
import com.xxx.springmvc.pojo.QueryVo;
import com.xxx.springmvc.service.ItemService;
//商品管理
@Controller //Springmvc的Controller是单例的,Struts2的Action是多例的。 接收请求参数不能用成员变量(属性驱动)接收,而是通过方法的形参获取。
public class ItemController {
@Autowired
private ItemService itemService;
//接收参数id;根据id修改商品
@RequestMapping(value = "/itemEdit.action") // .action可以省略(省略后不利于后期维护)。
public ModelAndView toEdit(HttpServletRequest request,HttpServletResponse response
,HttpSession session,Model model){
//原生Servlet时代开发
String id = request.getParameter("id");
//根据id查询商品
Items items = itemService.selectItemsById(Integer.parseInt(id));
ModelAndView mav = new ModelAndView();
//相当于往Request域中添加数据
mav.addObject("item", items); //jsp页面中可以通过el表达式${item}获取。
mav.setViewName("editItem"); //指定view视图(jsp页面)
return mav;
}
}
ItemController.java(Controller处理器,通过方法形参自动接收请求参数(简单类型)(形参不能直接接收List、数组、日期类型)):
package com.xxx.springmvc.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.xxx.springmvc.pojo.Items;
import com.xxx.springmvc.pojo.QueryVo;
import com.xxx.springmvc.service.ItemService;
//商品管理
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
//接收参数id;根据id修改商品
@RequestMapping(value = "/itemEdit.action")
//public ModelAndView toEdit(@RequestParam(value = "id",required = false,defaultValue = "1") Integer myId,
//@RequestParam() 表示请求参数与对应接收的方法形参不同名时,可以通过@RequestParam配置形参对应接收哪个请求参数名,以及设置默认值等。
public ModelAndView toEdit(Integer id,
HttpServletRequest request,HttpServletResponse response,HttpSession session,Model model){
//直接通过方法的形参id获取同名的请求参数。 形参类型建议使用基本类型对应的包装类型,因为可能会为null。
//处理器适配器会默认识别并进行赋值。
//根据id查询商品
Items items = itemService.selectItemsById(id);
ModelAndView mav = new ModelAndView();
mav.addObject("item", items);
mav.setViewName("editItem");
return mav;
}
}
ItemController.java(Controller处理器,接收参数并封装成对象(复杂类型).(List类型、数组类型)):
package com.xxx.springmvc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.xxx.springmvc.pojo.Items;
import com.xxx.springmvc.pojo.QueryVo;
import com.xxx.springmvc.service.ItemService;
//商品管理
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
//提交修改商品。 接收参数并封装成Items对象
@RequestMapping(value = "/updateitem.action")
public ModelAndView updateitem(Items items){ //形参可以是数组类型,来接收数组参数;但不能接收List类型。List类型必须通过POJO类或者包装类的属性来接收。
//通过形参items自动接收请求参数并封装成items对象。 (前端表单元素的name属性必须与Items实体类的属性保持一致,才能自动封装)
//如果对象属性(hobbyList)是List类型,前端表单元素的name可以设为hobbyList[0]来传参
//与方法形参的类型Items有关,与形参名items无关。
//修改
itemService.updateItemsById(items);
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
return mav;
}
}
ItemController.java(Controller处理器,接收参数并封装到POJO包装对象中(可以包装List、数组类型)):
package com.xxx.springmvc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.xxx.springmvc.pojo.QueryVo;
import com.xxx.springmvc.service.ItemService;
//商品管理
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
//提交修改商品。 (接收参数封装到POJO包装类型中)
@RequestMapping(value = "/updateitem.action")
public ModelAndView updateitem(QueryVo vo){
//前端表单元素的name属性值必须是items.xxx,接收参数时才能封装到QueryVo包装类的成员变量items中来。
//修改
itemService.updateItemsById(vo.getItems());
ModelAndView mav = new ModelAndView();
mav.setViewName("success");
return mav;
}
}
QueryVo.java(POJO包装类):
package com.xxx.springmvc.pojo;
public class QueryVo {
//商品 POJO实体对象
private Items items; //前端表单元素的name属性值必须是items.xxx,接收参数时才能封装到成员变量items中来。
public Items getItems() {
return items;
}
public void setItems(Items items) {
this.items = items;
}
}
web.xml(Web项目核心配置文件,解决POST传参乱码问题(国际化)):
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>springmvc-mybatis</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring监听器。(创建Spring容器,单例) -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 处理POST提交参数乱码问题 (国际化) -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- Springmvc 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 通过初始化参数,指定配置文件的位置。 默认找 /WEB-INF/[servlet的名称]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!--
1. /* 拦截所有,包括.jsp .js .png .css等。不推荐使用 (Struts2核心过滤器中的/*不会拦截.jsp页面等)
2. *.action *.do 拦截以.do .action 结尾的请求。 可以使用(一般用于后台)
3. / 拦截所有 (不拦截.jsp;但拦截.js .png .css等) 推荐使用(一般用于前台、面向消费者) (/ 需要对静态资源放行)
-->
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<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>
</web-app>