菜鸟学习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效果图。

三、总结。

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

时间: 2024-08-09 15:15:09

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

菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式

一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架也是界面把信息传输到Contorller层的一种架构,通过这个架构可以让我们把页面和Contorller层解耦,使得开发人员的分工更加明确. 二.代码演示. 1.首先配置SpringMVC环境. 1.1导入jar. 值得注意的是红色标记的commons-logging这个jar包一定得引入进去不然会

菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串

一.概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的. 二.代码展示 需要引用的jar包 1.xml配置 Web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.s

菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析

一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 web.xml. <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns=&

菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层

一.概述. SpringMVC中界面请求Contorller1,Contorller1需要重定向到Contorller2中显示其他页面或者做一些业务逻辑,Spring中提供了这个功能利用"redirect:/"来进行重定向. 二.代码演示. 1.界面 Login.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF

springMVC 注解版

关于Spring MVC注解 @Transactional 事务标签 @InitBinder 标签 分类: Java开发 源代码分享2012-06-14 10:59 7721人阅读 评论(2) 收藏 举报 springmvcjavaemailpathstring 主要用到了spring-aop-2.5.6.jar的AOP支持包! 之前我们在AccountService中加入了注解@Transactional标签,但是要想要真正发挥事务作用,还需要一些配置. 主要需要调整dao.xml文件 dao

菜鸟学习Spring——60s配置XML方法实现简单AOP

一.概述. 上一篇博客讲述了用注解的形式实现AOP如今讲述第二种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作參照上一篇博客<菜鸟学习Spring--60s使用annotation实现简单AOP> 文件夹结构: 事实上比起上一篇博客中用annotation来实现AOP的方式我们仅仅要把SecurityHandler.java和配置文件applicationContext.xml更改为以下内容就能够了.以下我把这两个文件的代码写下来. SecurityHandler.java

springMVC(注解版笔记)

springMVC(注解版) 较之于非注解版本,发生一下变化: 1.配置文件需要配置的标签有: <!-- 包的扫描,此包下面的所有包都启用注解 --> <context:component-scan base-package="com.mindreader.springmvc.controller" /> <!-- 开启注解 --> <!--包的映射--> <bean class="org.springframework.

javascript获取当前url中的参数

javascript获取当前页面url中的参数可以使用location的search方法,获取到的是url中?后面的部分,例如http:localhost:8080/Manager/index.jsp?id=1 使用location的search方法可以获取到字符串?id=1;想要获取?后面的键值对可以使用substring方法对其进行截取,截取后获得id=1;需要获得id的值,可以使用split()方法对其进行拆分,拆分表达式为"=".下面看具体例子: window.onload =

如何获取url中的参数并传递给iframe中的报表

在使用报表软件时,用户系统左边一般有目录树,点击报表节点就会在右侧网页的iframe中显示出报表,同时点击的时候也会传递一些参数给网页,比如时间和用户信息等.如何使网页中的报表能够获取到传递过来的参数呢?以下用报表软件FineReport简单介绍一些. 具体实现过程 将报表生成页面时,给网页添加onload事件,首先获取url中的参数,然后嫁接到iframe的src上,或者通过获得的参数拼接处完整的报表url赋给iframe的src. <html> <head> <title