zoukankan      html  css  js  c++  java
  • 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方法请求时会调用。

  • 相关阅读:
    Vue移动端调用高德获取当前定位城市
    var,let,const三者的区别
    JVM学习与总结一
    五层网络模型整理
    功能强大的CFR反编译工具
    jad 反编译 jar文件、批量、单个class文件,秒懂!
    [ERROR] 2020-08-03 10:18:11 [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:350) Context initialization failed
    TortoiseSVN的bin目录下没有 svn.exe 问题;Error running 'tomcat8.0.47': Unable to open debugger port (127.0.0.1:57422): java.net.SocketException "socket closed";端口被占用问题
    显示数据时,将同列不同行的数据合并到其中一行的sql
    java.lang.IllegalArgumentException: ServletContext must not be null
  • 原文地址:https://www.cnblogs.com/vincent4code/p/5126709.html
Copyright © 2011-2022 走看看