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

技术分享

技术分享

三、总结。

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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。