菜鸟学习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 ...
随机推荐
- 前端CSS的基本素养
前端开发的三驾马车——html.css.js,先谈谈CSS CSS 前期:解决布局.特效.兼容问题 中级:网站风格的制定.色调.模块.布局方式.交互方式.逻辑设计等 高级:模块命名.类的命名.文件的组 ...
- python 3中 的subprocess
commands好像python3.6被去除了,它现在被subprocess替代了 FUNCTIONS getoutput(cmd) Return output (stdout or stderr) ...
- 【解决】Linux Tomcat启动慢--Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [236,325] milliseconds
一.背景 今天部署项目到tomcat,执行./startup.sh命令之后,访问项目迟迟加载不出来,查看日志又没报错(其实是我粗心了,当时tomcat日志还没打印完),一开始怀疑是阿里云主机出现问题, ...
- vue脚手架的安装和使用
脚手架安装:
- Docker Run 设置环境变量
Docker Run We can then override the environment variables set in the Docker file when running the im ...
- bcdedit /copy {current} /d "xxx" 报错,提示找不到系统文件
步骤: cd c:windows/system32 bcdedit /set {default} osdevice boot bcdedit /set {default} device boot bc ...
- 关于webpack 以及 webpack配置和常用插件的学习记录 (2) ------ devServer
DevServer: devserver会启动一个http服务器用于服务网页请求,接收webpack发出的文件变化的信号.通过websocket协议自动刷新网页,实现实时预览. 安装: npm i w ...
- 04-树6 Complete Binary Search Tree (30 分)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- vm 中安装 CentOS7
第三步:安装ISO文件 1.在vm下,文件,新建虚拟机 在我的机算机中,选中刚命名的CentOS7,右键,属性 2.开启虚拟机 PS: 打开虚拟机之后,提示了一个小错误,LZ根据错误提示,到BIOS里 ...
- [转] Tomcat 禁用URL中的JSESSIONID
[From] http://stackoverflow.com/questions/962729/is-it-possible-to-disable-jsessionid-in-tomcat-serv ...