spring mvc 怎样接收formdata
表单的代码如下: pageEncoding="UTF-8"%> Add User //添加用户 function addUser() { var form = document.forms[0]; form.action = "${pageContext.request.contextPath}/user/addUser1"; //form.action = "${pageContext.request.contextPath}/user/addUser2"; //form.action = "${pageContext.request.contextPath}/user/addUser3"; form.method = "post"; form.submit(); } 账号 密码 1、直接把表单的参数写在Controller相应的方法的形参中 @RequestMapping("/addUser1") public String addUser1(String userName,String password) { System.out.println("userName is:"+userName); System.out.println("password is:"+password); return "/user/success"; } 2、通过HttpServletRequest接收 @RequestMapping("/addUser2") public String addUser2(HttpServletRequest request) { String userName = request.getParameter("userName"); String password = request.getParameter("password"); System.out.println("userName is:"+userName); System.out.println("password is:"+password); return "/user/success"; } 3、通过一个bean来接收 1)建立一个和表单中参数对应的bean public class User { 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; } } 2)用这个bean来封装接收的参数 @RequestMapping("/addUser3") public String addUser3(User user) { System.out.println("userName is:"+user.getUserName()); System.out.println("password is:"+user.getPassword()); return "/user/success"; } 4、通过json数据接收 pageEncoding="UTF-8"%> Add User $(document).ready(function(){ $("#button_submit").click(function(){ var name = $("#userName").val(); var pass = $("#password").val(); var user = {userName:name,password:pass};//拼装成json格式 $.ajax({ type:"POST", url:"${pageContext.request.contextPath}/user/addUser4", data:user, success:function(data){ alert("成功"); }, error:function(e) { alert("出错:"+e); } }); }); }); 账号 密码 依然可以使用bean来接收json数据 @RequestMapping("/addUser4") public String addUser4(User user) { System.out.println("userName is:"+user.getUserName()); System.out.println("password is:"+user.getPassword()); return "/user/success"; } 5、使用jQuery的serializeArray() 方法序列化表单元素 如果表单元素很多,手工拼装成json数据非常麻烦,可以使用jQuery提供的serializeArray()方法序列化表单元素,返回json数据结构数据。
pageEncoding="UTF-8"%> Add User $(document).ready(function(){ $("#button_submit").click(function(){ //序列化表单元素,返回json数据 var params = $("#userForm").serializeArray(); //也可以把表单之外的元素按照name value的格式存进来 //params.push({name:"hello",value:"man"}); $.ajax({ type:"POST", url:"${pageContext.request.contextPath}/user/addUser5", data:params, success:function(data){ alert("成功"); }, error:function(e) { alert("出错:"+e); } }); }); }); 账号 密码 依然可以使用bean来接收json数据: @RequestMapping("/addUser5") public String addUser5(User user) { System.out.println("userName is:"+user.getUserName()); System.out.println("password is:"+user.getPassword()); return "/user/success"; }
spring的ioc注入方式有几种
一、Set注入二、构造器注入三、静态工厂的方法注入内容拓展:一、Set注入1、这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一个SpringDao对象,那么就可以定义一个private的SpringDao成员变量,然后创建SpringDao的set方法(这是ioc的注入入口)。
2、随后编写spring的xml文件,中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性Springdao,所以要在标签中创建一个标签指定SpringDao。
标签中的name就是SpringAction类中的SpringDao属性名,ref指下面,这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入。
二、构造器注入1、这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来。
2、在XML文件中同样不用的形式,而是使用标签,ref属性同样指向其它标签的name属性。
三、静态工厂的方法注入1、静态工厂顾名思义,就是通过调用静态工厂的方法来获取自己需要的对象,为了让spring管理所有对象,我们不能直接通过"工程类.静态方法()"来获取对象,而是依然通过spring注入的形式获取。
2、同样看关键类,这里我需要注入一个FactoryDao对象,这里看起来跟第一种注入一模一样,但是看随后的xml会发现有很大差别。
如何设置Spring Boot中@RequestBody反序列化实体的默认值
可以的在web程序开发中,前端和服务器数据的传输方式常用的有两种,一种是以表单的形式提交,此时可以利用jquery的serialize()方法将表单内容转为a=1&b=2&c=3&d=4&e=5这样的格式传输过去,接收端则可以用javabean直接接收。
还有一种方式是以json格式传输,接收时若直接用bean接收则接收不到,此时应该用@RequestBody方式,需要注意的是接收的需要是json串,而不是json对象,可以在发送前使用JSON.stringify函数进行处理。
例子如下客户端:var url=path+'testConverter.html';$.ajax({url :url,type :"POST", dataType:"json",contentType:'application/json;charset=utf-8',data:JSON.stringify({userId:'1',userName:'hello',password:'test',credits:'2',lastIp:'',lastVisit:'1986-05-27'}),success :function(data) {alert(data.userName); },error:function(e){alert("err"); } 服务端:@RequestMapping(value="/testConverter.html")@ResponseBodypublic UsertestConverter(@RequestBody User user){System.out.println(user.getUserName());user.setUserName("testname"); returnuser; }上面例子中服务端返回的是个对象,@ResponseBody函数会自动将其转换为客户端要求的'dataType:"json",'格式。
最后需要注意的是1,在xxx-serverlet配置文件中应该写上,以便可以使用@ResponseBody和@RequestBody2,发送时要写上 contentType:'application/json'3,数组内容要用[]而不是list,用list会接收到一个map对象,而不是bean-
SpringMVC后台获取前台参数的几种方式
读取请求参数1、直接把表单的参数写在Controller相应的方法的形参中案例:/*** 1.直接把表单的参数写在Controller相应的方法的形参中*/@RequestMapping("/add")public String add(String username,String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "index";}2、通过HttpServletRequest接收案例:/*** 2、通过HttpServletRequest接收*/@RequestMapping("/add")public String add(HttpServletRequest request) {String username=request.getParameter("username");String password=request.getParameter("password");System.out.println("username is:"+username);System.out.println("password is:"+password);return "index";}3、通过一个bean来接收案例:public class User {private Long id;private String username;private String password;public User(){}public Long getId() {return id;}public String getUsername() {return username;}public String getPassword() {return password;}public void setId(Long id) {this.id = id;}public void setUsername(String username) {this.username = username;}public void setPassword(String password) {this.password = password;}}/*** 3、通过一个bean来接收*/@RequestMapping("/add")public String add(User user) {System.out.println("username is:"+user.getUsername());System.out.println("password is:"+user.getPassword());return "index";}4、通过@PathVariable获取路径中的参数案例:/*** 4、通过@PathVariable获取路径中的参数* @param username* @param password* @return*/@RequestMapping(value="/add/{username}/{password}",method=RequestMethod.GET)public String addUser4(@PathVariable String username,@PathVariable String password) {System.out.println("username is:"+username);System.out.println("password is:"+password);return "index";}
springmvc作为后台该如何接收并返回数据
1、直接把表单的参数写在Controller相应的方法的形参中 案例:/*** 1.直接把表单的参数写在Controller相应的方法的形参中*/ @RequestMapping("/add") public String add(String username,String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; }2、通过HttpServletRequest接收 案例:/*** 2、通过HttpServletRequest接收*/ @RequestMapping("/add") public String add(HttpServletRequest request) { String username=request.getParameter("username"); String password=request.getParameter("password"); System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; }3、通过一个bean来接收 案例:public class User { private Long id; private String username; private String password; public User(){} public Long getId() { return id; } public String getUsername() { return username; } public String getPassword() { return password; } public void setId(Long id) { this.id = id; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } }/*** 3、通过一个bean来接收*/ @RequestMapping("/add") public String add(User user) { System.out.println("username is:"+user.getUsername()); System.out.println("password is:"+user.getPassword()); return "index"; }4、通过@PathVariable获取路径中的参数 案例:/*** 4、通过@PathVariable获取路径中的参数* @param username* @param password* @return*/ @RequestMapping(value="/add/{username}/{password}",method=RequestMethod.GET) public String addUser4(@PathVariable String username,@PathVariable String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return "index"; } 访问路径 http://localhost/Demo/demo/add/cy/111111
请问springMVC中,自定义参数绑定,怎么确定那个参数要转换的
1.首先在创建应用对象时引入autoConfig?package com;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}?2.其次配置文件?######primary############# datasource.primary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNowLog datasource.primary.username=sa datasource.primary.password=xxxxxx datasource.primary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver ######secondary############# datasource.secondary.url=jdbc:sqlserver://xx.xx.xx.xx:1433;DatabaseName=PlayNow_New datasource.secondary.username=sa datasource.secondary.password=xxxxxx datasource.secondary.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver ?3.再其次是数据源的创建?@Configuration public class GlobalDataConfiguration { @Bean(name="primaryDataSource")
如何利用spring data mongodb 进行多条件查询
@Override public long findBlogTodayAcceccCountByUserId(String userId) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); Date todayStart = calendar.getTime(); calendar.add(Calendar.DATE, 1); Date endStart = calendar.getTime(); Query query = new Query(); Criteria criteria = Criteria.where("createTime").gte(todayStart).lte(endStart); query.addCriteria(criteria); return mongoTemplate.count(query, getEntityClass()); }
springmvc requestbody可以接收字符串吗
简介:@RequestBody作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;ii) 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。
使用时机:A) GET、POST方式提时, 根据request header Content-Type的值来判断:application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);其他格式, 必须(其他格式包括application/json, application/xml等。
这些格式的数据,必须使用@RequestBody来处理);B) PUT方式提交时, 根据request header Content-Type的值来判断:application/x-www-form-urlencoded, 必须;multipart/form-data, 不能处理;其他格式, 必须;说明:request的body部分的数据编码格式由header部分的Content-Type指定;@ResponseBody作用: 该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
eclipse如何搭建springmvc +mybatis
展开全部 SpringMVC+MyBatis+Freemarker 简单框架搭建(一)一、开发环境: Eclipse、Tomcat、SVN等请参见如下的帖子,很详细了。
http://www.iteye.com/topic/982182 svn和maven插件的安装: 1、先安装gef插件 地址:http://download.eclipse.org/tools/gef/updates/interim/ 2、安装svn插件 地址:http://subclipse.tigris.org/update_1.6.x 3、maven插件 m2eclipse-core Update 地址: http://m2eclipse.sonatype.org/sites/m2e m2eclipse-extras Update 地市: http://m2eclipse.sonatype.org/sites/m2e-extras 4、安装可能出现的问题 直接在线安装maven2 会出现依赖插件找不到的问题,无法安装。
必须先安装gef 插件后才能安 装m2eclipse-core 插件,然而安装m2eclipse-extras 插件又依赖subclipse 插件。
所以,三个插 件的正确的安装顺序是:gef插件 》subclipse插件 》m2eclipse插件二、web.xml 和 applicationContext.xml 配置 1、web.xml文件配置: contextConfigLocation classpath:applicationContext.xml springServlet org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:contextConfigLocation-springService.xml 0 springServlet / org.springframework.web.context.ContextLoaderListener org.springframework.web.util.IntrospectorCleanupListener 20 index.jsp Set Character Encoding org.springframework.web.filter.CharacterEncodingFilter encoding GB2312 forceEncoding true Set Character Encoding / Archetype Created Web Application节点和节点是web项目启动时最先读取的两个节点,所以这两个节点里面所配置的内容是web项目启动时最先加载的部分。
节点中配置的applicationContext.xml里面主要是数据库的配置信息,以及事务等等 节点配置的是web请求的监听器 2、applicationContext.xml 内容如下: --...
转载请注明出处51数据库 » spring usernamepassw