Springmvc 前台传递参数到后台需要数据绑定
我们知道,当提交表单时,controller会把表单元素注入到command类里,但是系统注入的只能是基本类型,如int,char,String。但当我们在command类里需要复杂类型,如Integer,date,或自己定义的类时,controller就不会那么聪明了。这时,就需要我们帮助他们了。
后台controller:
@Controller
@RequestMapping(value = { "/projects/project" })
public class ProjectsController {
@Autowired
ProjectsService projectService;
@Autowired
UniskUserService userService;
@Autowired(required = false)
UserAndUserGroupService ugroupService;
public static final String path = "sys/modules/project/";
private static final Logger logger = LoggerFactory
.getLogger(ProjectsController.class);
/*
* 日期绑定转化
*/
@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
//Projects projects
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
setValue(sdf.parse(text));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//controller
/*
* 修改子项目实现
*/
@ResponseBody
@RequestMapping(value = "editSub", method = RequestMethod.POST)
public String editSub(HttpServletRequest request,HttpServletResponse response,@ModelAttribute("user") UniskUser user,@ModelAttribute("projects") Projects project, ModelMap map) throws UniskException {
projectService.update(project);
Map<String, String> result = new HashMap<String, String>();
result.put("resultCode", "0");
result.put("msg", "操作成功!");
System.out.println("edit");
return JsonUtil.toJson(result);
}
}
前台表单:
<div class="control-group">
<label class="control-label">项目发布时间:</label>
<div class="controls">
<input type="text" readonly id="starttime" name="starttime" maxlength="50" class="required times" placeholder="<fmt:formatDate value='${project.starttime }' pattern='yyyy-MM-dd'/>" />
</div>
</div>
<div class="control-group">
<label class="control-label">项目众筹时间:</label>
<div class="controls">
<input type="text" readonly id="crowdtime" name="crowdtime" maxlength="50" class="required times" placeholder="<fmt:formatDate value='${project.crowdtime }' pattern='yyyy-MM-dd'/>" />
</div>
</div>
<div class="control-group">
<label class="control-label">项目验收时间:</label>
<div class="controls">
<input type="text" readonly id="examinetime" name="examinetime" maxlength="50" class="required times" placeholder="<fmt:formatDate value='${project.examinetime }' pattern='yyyy-MM-dd'/>" />
</div>
</div>
<div class="control-group">
<label class="control-label">项目结束时间:</label>
<div class="controls">
<input type="text" readonly id="endtime" name="endtime" maxlength="50" class="required times" placeholder="<fmt:formatDate value='${project.endtime }' pattern='yyyy-MM-dd'/>" />
</div>
</div>
getAsText和setAsText是要从新定义的。其中getAsText方法在get方法请求时会调用,而setAsText方法在post方法请求时会调用。
Springmvc 前台传递参数到后台需要数据绑定的更多相关文章
- SpringMvc的传递参数方式 -- url / requestMapping
在使用spring的项目中,前台传递参数到后台是经常遇到的事, 我们必须熟练掌握一些常用的参数传递方式和注解的使用,废话少说,直接上正文. 1. @requestMapping: 类级别和方法级别的注 ...
- SpringMVC中,前台jsp封装参数,绑定参数,传递参数到后台controller的过程详解
前台到后台的流程:前台jsp->后台:controller控制器层->service业务层->DAO数据访问层->数据库model模型层. 从上面流程可知,前台jsp的数据,想 ...
- angularjs-$http.post请求传递参数,后台Controller接受不到原因
现象回显 js文件 app.controller('indexCtrl', function($scope, $state, $http) { $scope.login = function() { ...
- 【前台 ajax】web项目前台传递数组给后台 两种方式
项目使用maven springMVC 有需求 将前台的数组 在ajax中 送给后台 方式1: 前台代码:[注意:ajax中的属性---traditional:true, ] 如果Post ...
- SpringMVC重定向传递参数
在SpringMVC的一个controller中要把参数传到页面,只要配置视图解析器,把参数添加到Model中,在页面用el表达式就可以取到.但是,这样使用的是forward方式,浏览器的地址栏是不变 ...
- SpringMVC 页面传递参数到controller的五种方式
一共是五种传参方式: 一:直接将请求参数名作为Controller中方法的形参 public String login (String username,String password) : 解 ...
- SpringMVC 前端传递list到后台
---恢复内容开始--- 1.前端获取传入后台的list 2.ajax写法: $.ajax({ type: 'post', url: url, async:false, dataType:" ...
- springMVC通过ajax传递参数list对象或传递数组对象到后台
springMVC通过ajax传递参数list对象或传递数组对象到后台 环境: 前台传递参数到后台 前台使用ajax 后台使用springMVC 传递的参数是N多个对象 JSON对象和JSON字符串 ...
- 解决axios传递参数后台无法接收问题
1.根据下面几个方法改变前台传递参数方式 这样后台就可以直接根据传递的参数获取数据,如下图用户登录时直接传递用户名和密码 2.不改变前台传递样式修改后台接收方式
随机推荐
- OpenJudge_2757:最长上升子序列
描述一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对于给定的一个序列(a1, a2, ..., aN),我们可以得到一些上升的子序列(ai1 ...
- (转)配置Spring管理的bean的作用域
http://blog.csdn.net/yerenyuan_pku/article/details/52833477 Spring管理的bean的作用域有: singleton 在每个Spring ...
- CentOS 6.5下安装Python 3.5.2(与Python2并存)
CentOS 6.5下安装Python 3.5.2(与Python2并存) 安装步骤 1,准备编译环境(环境不对,在安装过程中可能遇到各种问题.比如wget无法下载链接的文件) yum groupin ...
- ping ip
def ip_and_time(): """ get ip to ping from ip.txt then return two list , each ip that ...
- ASP.NetCore 错误 NU1605 检测到包降级: Microsoft.Data.Sqlite 从 2.2.1 降级到 2.1.0
找到使用的.csproj文件 将 <PackageReference Include="Microsoft.Data.Sqlite" Version="2.1.0& ...
- 函数内部属性之arguments和this
在函数内部,有两个特殊的对象:arguments和this. 1.arguments arguments是一个类数组对象.包含着传入函数中的所有参数.但这个对象还有一个名叫callee的属性,该属性是 ...
- 【亲测可行】Dev c++调试、运行报错解决方法总结
一.编译后 0错误 0警告,但是开始出现‘‘停止运行’’或者进行输入时出现‘‘停止运行’’ 可能的原因: 结构体指针为空,但调用了其成员. 有些scanf语句中忘记添加取址符. 无法跳出递归. 二. ...
- tornado框架基础03-请求与响应
01 请求与响应 请求 浏览器在发送请求的时候,会发送具体的请求信息,由请求行,请求消息头,请求正文 请求消息头 向服务器传递附加信息 Accept: 浏览器可以接受的MIME类型. Accept-C ...
- python 学习总结1
计算机与程序设计 一.计算机的概念 1.计算机是根据指令操作数据的设备. 2.计算机主要包括两个功能性一个是功能性另一个是计算性 功能性是对数据的操作,表现为数据计算,输入输出处理和结果存储 可编程性 ...
- Cisco路由器交换机基础配置
交换机配置 交换机基本状态: switch: # ROM状态, 路由器是rommon> hostname> # 用户模式 hostname# # 特权模式 hostname(config) ...