菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体
一、概述
将URL中参数转成实体在我们项目中用的很多比如界面提交表单请求后台的Contorller的时候通过URL传递了一串参数到后台,后台通过Spring让界面的字段与实体的字段映射来实现给后台的实体属性赋值。
二、代码演示。
2.1 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<filter>
<filter-name>encodingFilter</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>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.spring</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> </web-app>2.2springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
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.1.xsd"> <context:component-scan base-package="com.gaowei.ParamToObject" /> </beans>2.3test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> </head>
<body>
<form action="paramToEntity.spring" method="POST">
username:
<input type="text" name="username">
<br/>
password:
<input type="text" name="password">
<br/>
<input type="submit" name="submit">
<br/>
</form>
</body>
</html>2.4后台代码。
Userinfo.java
package com.gaowei.entity; public class Userinfo {
private String username; private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }ParamToEntity.java
package com.gaowei.ParamToObject; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.gaowei.entity.Userinfo; @Controller
public class ParamToEntity { @RequestMapping(value="paramToEntity",method=RequestMethod.POST)
public String paramToEntity(Userinfo userinfo){
System.out.println("username value="+userinfo.getUsername());
System.out.println("password value="+userinfo.getPassword());
return "test.jsp";
}
}2.5效果图。
三、总结。
前台向后台传递数据有很多种方式,我们可以根据不同的情况用不同的方法这也让我意识到以后我们要开发框架的时候要给使用者提供很多不同的方式来对应不同的情况。
菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体的更多相关文章
- 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串
一.概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的. 二.代码 ...
- 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式
一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...
- 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析
一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 we ...
- 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层
一.概述. SpringMVC中界面请求Contorller1,Contorller1需要重定向到Contorller2中显示其他页面或者做一些业务逻辑,Spring中提供了这个功能利用"r ...
- 【转】SpringBoot处理url中的参数的注解
1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMa ...
- SpringBoot处理url中的参数的注解
1.介绍几种如何处理url中的参数的注解 @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注解,是 @RequestMa ...
- springMVC 注解版
http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...
- 菜鸟学习Spring——60s配置XML方法实现简单AOP
一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...
- springMVC(注解版笔记)
springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:compon ...
随机推荐
- c语言和c++的相互调用
1.c与c++编译方式 (1)gcc和g++都可以编译.c文件,也都可以编译.cpp文件.g++和gcc是通过后缀名来辨别是c程序还是c++程序的(这一点与Linux辨别文件的方式不同,Linux是通 ...
- 洛谷 P1579 哥德巴赫猜想(升级版)
嗯... 这或许也算一道数论题吧... 题目链接:https://www.luogu.org/problemnew/show/P1579 这道题的说明好像只会扰乱人的思路....然后就是这道题的细节比 ...
- 20165224 陆艺杰 《Java程序设计》课程总结
每周作业链接汇总 https://www.cnblogs.com/lyj-/p/8414278.html https://www.cnblogs.com/lyj-/p/8695018.html htt ...
- web图标icon tomcat图标icon
有时候我们需要更改项目图标为自己的,毕竟tomcat的小猫图标用着不得劲. 你看打开百度等网站时,在地址栏前面就会出现一个蓝色的熊掌状图标或者其他的样式的图标,如果在桌面新建此链接的快捷方式,则桌面图 ...
- 老男孩python作业2-购物车程序
购物车程序要求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 ...
- POJ_1984 Navigation Nightmare 【并查集】
一.题面 POJ1984 二.分析 这题还是比较有意思的一题. 首先需要清楚的是,这题与普通并查集的区别在于它的节点之间的权值是二维的,因为是曼哈顿距离,肯定不能直接存距离,这样将不利于后面的路径压缩 ...
- 计算hashCode通用计算公式
1.java计算公式 @Override public int hashCode() { //设置初始值 ; //假设有效域为: name,age,idCardNo,incomeAnnual,sex, ...
- hdu1022 模拟栈
Train Problem I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- shell编程下
第1章 Whicle 1.1 while循环语句 在编程语言中,while循环(英语:while loop)是一种控制流程的陈述.利用一个返回结果为布林值(Boolean)的表达式作为循环条件,当这个 ...
- 设置Yii2发生错误返回json
如果指示指定一个函数那么可以使用: \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 如果想整个应用都返回JSO ...