springmvc2

回顾什么是springmvc,它与spring有什么关系

springmvc属于spring框架的后续产品,用在基于MVC的表现层开发,类似于struts2框架

参见<<springmvcspring的关系.JPG>>

 

 

回顾springmvc工作流程

参见<< springmvc工作流.JPG>>

 

 

第十四章 springmvc快速入门(注解版本)

1springmvc快速入门(传统版)

   步一:创建springmvc-day02这么一个web应用

   步二:导入springiocspringwebspringmvc相关的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

 

步三:在/WEB-INF/下创建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>

步四:创建HelloAction.java控制器类

@Controller

public class HelloAction{

@RequestMapping(value="/hello")

public String helloMethod(Model model) throws Exception{

System.out.println("HelloAction::helloMethod()");

model.addAttribute("message","这是我的第二个springmvc应用程序");

return "/success.jsp";

}

}

步五:在/WebRoot/下创建success.jsp

<%@ page language="java" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <title>这是我的第二个springmvc应用程序</title>

  </head>

  <body>

success.jsp<br/>

${message}

  </body>

</html>

步六:在/src/目录下创建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.season.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>

步七:部署web应用到tomcat中,通过浏览器访问如下URL

       http://127.0.0.1:8080/springmvc-day02/hello.action

 

 

第十五章 一个Action中,可以写多个类似的业务控制方法

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

 

 

第十六章 在业务控制方法中写入普通变量收集参数

1)可以在业务控制方法中,以参数形式收集客户端参数,springmvc采用方法参数形式的

@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

   

 

第十七章 限定某个业务控制方法,只允许GETPOST请求方式访问

1)可以在业务控制方法前,指明该业务控制方法只能接收GETPOST的请求

@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的话,GETPOST请求都支持

 

 

第十八章 在业务控制方法中写入RequestResponse等传统web参数

1)可以在业务控制方法中书写传统web参数,这种方式我们不提倡,耦合了

@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");

}

}

 

 

第十九章 在业务控制方法中写入模型变量收集参数,且使用@InitBind来解决字符串转日期类型

1) 在默认情况下,springmvc不能将String类型转成java.util.Date类型,所有我们只能在Action

中自定义类型转换器

<form action="${pageContext.request.contextPath}/user/add.actionmethod="POST">

编号:<input type="text" name="id" value="${id}"/><br/>

姓名:<input type="text" name="name" value="${name}"/><br/>

薪水:<input type="text" name="sal" value="${sal}"/><br/>

入职时间:<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";

}

}

 

 

第二十章 在业务控制方法中写入UserAdmin多个模型收集参数

1) 可以在业务控制方法中书写1个模型来收集客户端的参数

2) 模型中的属性名必须和客户端参数名一一对应

3) 这里说的模型不是Model对象,Model是向视图中封装的数据

@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";

}

}

 

 

第二十一章 在业务控制方法中写入包装User的模型来收集参数

可以在业务控制方法中书写0个或多个模型来收集客户端的参数

1) 如果多个模型中有相同的属性时,可以用user.nameadmin.name来收集客户端参数

2) 用一个新的模型将UserAdmin再封装一次

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.actionmethod="POST">

编号:<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/>

入职时间:<input type="text" name="user.hiredate" value='<fmt:formatDate value="${bean.user.hiredate}" type="both" />'/><br/>

<input type="submit" value="注册"/>

</form>

 

 

第二十二章 在业务控制方法中收集数组参数

批量删除用户

@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("需要删除的id为:");

for(int id : ids){

System.out.print(id+" ");

}

model.addAttribute("message","批量删除成功");

return "/success.jsp";

}

}

 

 

第二十三章 在业务控制方法中收集List<JavaBean>参数

批量注册用户

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.actionmethod="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>

 

 

第二十四章 结果的转发和重定向

1) 在转发情况下,共享request域对象,会将参数从第一个业务控制方法传入第二个业务控制方法,

反之,重定向则不行

删除id=10号的用户,再查询用户

@Controller

@RequestMapping(value="/user")

public class UserAction {

 

@RequestMapping(value="/delete")

public String delete(int id) throws Exception{

System.out.println("删除用户->" + id);

//转发到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";

}

}

 

 

第二十五章 异步发送表单数据到JavaBean,并响应JSON文本返回

1) 提交表单后,将JavaBean信息以JSON文本形式返回到浏览器

bean2json.jsp

<form>

编号:<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.season.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>

 

 

第二十六章 员工管理系统--查询员工

1springmvc + spring + jdbc + oracle

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值