SSM学习10Spring-MVC学习数据绑定01
个人理解:数据绑定就是后台获取前台的数据。
1.绑定默认数据类型:
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。依旧是用idea进行创建项目,项目目录:
src下创建com.zyk.controller包,在包下新建Usercontroller.java:
1 package com.zyk.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import com.zyk.po.Order; 6 import com.zyk.po.User; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 11 @Controller 12 public class UserController { 13 // 绑定默认数据类型 14 @RequestMapping("/selectUser") 15 public String selectUser(HttpServletRequest request){ 16 String id=request.getParameter("id"); 17 System.out.println("id="+id); 18 return "success"; 19 } 20 21 22 23 }
启动tomcat,在浏览器中输入:http://localhost:8080/springMvc02_war_exploded/selectUser?id=1
可以看到界面转至success.jsp界面,控制台输出了id=1
2.绑定简单数据
修改controller.java文件:
1 package com.zyk.controller; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import com.zyk.po.Order; 6 import com.zyk.po.User; 7 import org.springframework.stereotype.Controller; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestParam; 10 11 @Controller 12 public class UserController { 13 // 绑定默认数据类型 14 // @RequestMapping("/selectUser") 15 // public String selectUser(HttpServletRequest request){ 16 // String id=request.getParameter("id"); 17 // System.out.println("id="+id); 18 // return "success"; 19 // } 20 21 22 // 绑定简单数据 23 @RequestMapping("/selectUser") 24 public String selectUser(@RequestParam(value="user_id")int id){ 25 // String id=request.getParameter("id"); 26 System.out.println("id="+id); 27 return "success"; 28 } 29 30 31 }
@RequestParam是进行间接绑定的注解,请求中ser_id参数的值赋给方法中的id形参,最后输出。
实验结果与上面一样。
3.绑定pojo类型
pojo个人理解为原来javaweb中的javabean。
新建com.zyk.po包,在包里新建User类:
1 package com.zyk.po; 2 3 //绑定普通pojo 4 public class User { 5 private int id; 6 7 public int getId() { 8 return id; 9 } 10 11 public void setId(int id) { 12 this.id = id; 13 } 14 15 public String getUsername() { 16 return username; 17 } 18 19 public void setUsername(String username) { 20 this.username = username; 21 } 22 23 public int getPassword() { 24 return password; 25 } 26 27 public void setPassword(int password) { 28 this.password = password; 29 } 30 31 private String username; 32 private int password; 33 }
修改UserController类:
package com.zyk.controller; import javax.servlet.http.HttpServletRequest; import com.zyk.po.Order; import com.zyk.po.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class UserController { // //绑定pojo值 @RequestMapping("/toRegister") public String toRegister(){ return "register"; } @RequestMapping("/registerUser") public String registerUser(User user){ String username=user.getUsername(); int password=user.getPassword(); System.out.println("username="+username); System.out.println("password"+password); return "success"; } }
在jsp下新建register.jsp:
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <html> 3 <head> 4 <title>注册</title> 5 </head> 6 <body> 7 <form action="${pageContext.request.contextPath}/registerUser" method="post"> 8 用户名:<input type="text" name="username"><br> 9 密码:<input type="password" name="password"><br> 10 <input type="submit" value="注册"> 11 </form> 12 </body> 13 </html>
启动tomcat,浏览器中输入:http://localhost:8080/springMvc02_war_exploded/toRegister
页面转至register.jsp界面:
输入用户名和密码,看能否绑定数据:
点击注册后,界面转移至success.jsp中且控制台有输出,说明绑定成功:
4.绑定包装的pojo
在com.zyk.po下新建Order:
package com.zyk.po; //绑定包装pojo,Order中不仅有int类型的基本数据,还有用户pojo public class Order { private int orderId; public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } private User user; }
可以看出Order中,有User类型的数据。
修改UserController
package com.zyk.controller; import javax.servlet.http.HttpServletRequest; import com.zyk.po.Order; import com.zyk.po.User; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @Controller public class UserController { // //绑定包装的pojo值 @RequestMapping("/tofindOrdersWithUser") public String tofindOrdersWithUser(){ return "orders"; } @RequestMapping("/findOrderWithUser") public String findOrderWithUser(Order order){ int orderId=order.getOrderId(); User user=order.getUser(); String username=user.getUsername(); System.out.println("orderid="+orderId); System.out.println("username="+username); return "success"; } }
在jsp下新建orders.jsp:
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/htm14/1oose.dtd"> 3 <html> 4 <head> 5 <title>查询订单</title> 6 </head> 7 <body> 8 <form action="${pageContext.request.contextPath}/findOrderWithUser" method="post"/> 9 订单编号:<input type="text" name="orderId"><br> 10 所属单位:<input type="text" name="user.username"><br> 11 <input type="submit" value="查询"> 12 </form> 13 </body> 14 </html>
启动tomcat,浏览器中输入:http://localhost:8080/springMvc02_war_exploded/tofindOrdersWithUser
页面转至orders.jsp中:
输入数据后,点击查询,随后跳转至success.jsp中,控制台中有输出证明成功:
数据绑定中出现的问题:
1.汉字刚开始是乱码的,随后需要在web.xml中配置<filter>标签。
配置完成的web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!-- <context-param>--> <!-- <param-name>contextConfigLocation</param-name>--> <!-- <param-value>/WEB-INF/applicationContext.xml</param-value>--> <!-- </context-param>--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <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> </web-app>
2.绑定包装的pojo时,如果查询条件参数是包装类的直接基本属性,则参数名直接用对应的属性名。如果查询条件参数是包装类中 POJO 的子属性,则参数名必须为[对象属性],其中[对象] 要和包装 POJO 中的对象属性名称一致, [属性]要和包装 POJO 中的对象属性一致。
本次测试的完全目录:
由于开始使用IDEA进行的开发,可能有些人没有安装IDEA或者没有配置IDEA的tomcat等,但是IDEA确实方便很多,还有第一次用IDEA搭建的web.xml报错了,需要注释掉<context-param>标签,总之粘贴我所写的web.xml就行了。
