菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体

一、概述
       将URL中参数转成实体在我们项目中用的很多比如界面提交表单请求后台的Contorller的时候通过URL传递了一串参数到后台,后台通过Spring让界面的字段与实体的字段映射来实现给后台的实体属性赋值。
二、代码演示。

2.1 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://xmlns.jcp.org/xml/ns/javaee"   
  4.     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"  
  5.     id="WebApp_ID" version="3.1">  
  6. <filter>  
  7.     <filter-name>encodingFilter</filter-name>  
  8.       
  9.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  10.       
  11.     <init-param>  
  12.         <param-name>encoding</param-name>  
  13.         <param-value>utf-8</param-value>  
  14.     </init-param>  
  15. </filter>  
  16. <filter-mapping>  
  17.     <filter-name>encodingFilter</filter-name>  
  18.     <url-pattern>/*</url-pattern>  
  19. </filter-mapping>  
  20.   
  21. <servlet>  
  22.         <servlet-name>springMVC</servlet-name>  
  23.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  24.         <load-on-startup>1</load-on-startup>  
  25.     </servlet>  
  26.     <servlet-mapping>  
  27.         <servlet-name>springMVC</servlet-name>  
  28.         <url-pattern>*.spring</url-pattern>  
  29.     </servlet-mapping>  
  30.     <welcome-file-list>  
  31.         <welcome-file>index.jsp</welcome-file>  
  32.     </welcome-file-list>  
  33.   
  34. lt;/web-app>  



2.2springMVC-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="  
  6.         http://www.springframework.org/schema/beans   
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  8.         http://www.springframework.org/schema/context   
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  10.           http://www.springframework.org/schema/mvc   
  11.         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">  
  12.       
  13.     <context:component-scan base-package="com.gaowei.ParamToObject" />  
  14.   
  15.       
  16.   
  17. </beans>  



2.3test.jsp

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8.   
  9. </head>  
  10. <body>  
  11.     <form action="paramToEntity.spring" method="POST">  
  12.         username:  
  13.         <input type="text" name="username">  
  14.         <br/>  
  15.         password:  
  16.         <input type="text" name="password">  
  17.         <br/>  
  18.         <input type="submit" name="submit">  
  19.         <br/>  
  20.     </form>  
  21. </body>  
  22. </html>  


2.4后台代码。

Userinfo.java

  1. package com.gaowei.entity;  
  2.   
  3. public class Userinfo {  
  4.     private String username;  
  5.       
  6.     private String password;  
  7.   
  8.     public String getUsername() {  
  9.         return username;  
  10.     }  
  11.   
  12.     public void setUsername(String username) {  
  13.         this.username = username;  
  14.     }  
  15.   
  16.     public String getPassword() {  
  17.         return password;  
  18.     }  
  19.   
  20.     public void setPassword(String password) {  
  21.         this.password = password;  
  22.     }  
  23.       
  24.       
  25. }  


ParamToEntity.java

  1. package com.gaowei.ParamToObject;  
  2.   
  3. import org.springframework.stereotype.Controller;  
  4. import org.springframework.web.bind.annotation.RequestMapping;  
  5. import org.springframework.web.bind.annotation.RequestMethod;  
  6.   
  7. import com.gaowei.entity.Userinfo;  
  8.   
  9. @Controller  
  10. public class ParamToEntity {  
  11.   
  12.     @RequestMapping(value="paramToEntity",method=RequestMethod.POST)  
  13.     public String paramToEntity(Userinfo userinfo){  
  14.         System.out.println("username value="+userinfo.getUsername());  
  15.         System.out.println("password value="+userinfo.getPassword());  
  16.         return "test.jsp";  
  17.     }  
  18. }  


2.5效果图。

三、总结。

       前台向后台传递数据有很多种方式,我们可以根据不同的情况用不同的方法这也让我意识到以后我们要开发框架的时候要给使用者提供很多不同的方式来对应不同的情况。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值