发表于: 2018-03-21 21:55:55

1 628


今日完成

1.解决昨天遇到的问题;

  (1)模糊查询分页时加入参数绑定,通过@RequestParam进行筛选

  1. 当URL指向的是某一具体业务资源(或者资源列表),例如博客,用户时,使用@PathVariable
  2. 当URL需要对资源或者资源列表进行过滤,筛选时,用@RequestParam
  3. 一旦我们在方法中定义了@RequestParam变量,如果访问的URL中不带有相应的参数,就会抛出异常——这是显然的,Spring尝试帮我们进行绑定,然而没有成功。但有的时候,参数确实不一定永远都存在,这是我们可以通过定义required属性:

    @RequestParam(name="id",required=false)

    当然,在参数不存在的情况下,可能希望变量有一个默认值:

    @RequestParam(name="id",required=false,defaultValue="0")

        感觉可以通过第三点返回分页查询时的查询页.将当前页的值放在URL中传递.明天尝试;

@RequestMapping(value = "/student/dynamic")
public String selectByName(@RequestParam(value = "currentPage",defaultValue ="1",required = false)int currentPage,
                          @RequestParam(value ="name" ) String name,Model model)
throws IOException {
PageBean<DateTypeChange1> list =stuServiceImpl.findUserByName(currentPage,name);
   String n=name;
   model.addAttribute("pagemsg2",list);//回显分页数据
   model.addAttribute("name", n);
   return "listbyname";
}

上图中将name属性写在URL传递给下一次查询.


(2)json-taglib这个jar包无法通过maven的pom导入的问题

  在CMD中进入jar包所在路径输入下面的命令行,手动导入jar包:

mvn install:install-file -Dfile=json-taglib-0.4.1.jar -DgroupId=atg.taglib.json -DartifactId=json-taglib -Dversion=0.4.1 -Dpackaging=jar -DgeneratePom=true


@RequestMapping("/student/jsontest")
public String jsontest(Model model) throws IOException {

student=stuServiceImpl.findUserById(6l);
   model.addAttribute("student",student);
   return "jsontest";
}


<html>
<head>
   <title>Title</title>
</head>
<body>
<json:object>
<json:property  name="ID" value="${student.ID}"/>
<json:property  name="姓名" value="${student.name}"/>
<json:property  name="QQ" value="${student.QQ}"/>
<json:property  name="入学时间" value="${student.time_of_enrollment}"/>
</json:object>
</body>
</html>


2.springMVC的文件上传

 @RequestMapping(value = "/fileSave", method = RequestMethod.POST)
public String filesave(Model model, MultipartFile file, HttpServletRequest request){
String path = request.getSession().getServletContext().getRealPath("/files");
       String fileName=file.getOriginalFilename();
       System.out.println(path);
       File targetFile = new File(path, fileName);
       if (!targetFile.exists()) {
targetFile.mkdirs();
       }
try {
file.transferTo(targetFile);
       } catch (IOException e) {
e.printStackTrace();
       }
model.addAttribute("fileUrl", request.getContextPath()+"/upload/"+fileName);
       return "result";
   }
}

尝试参照例子写了上面的controller,但是文件未传入上面代码中的file,反而在request中体现了出来.应该是映射器未配置好的锅.

明天计划

  AOP该看了;

  完成文件上传;

遇到问题

写在了今日完成第二条中;

收获

关于springMVC的参数绑定有了新的认识.


返回列表 返回列表
评论

    分享到