SpringMVC学习记录(八)--开发中的小问题
来源:互联网 发布:福州靠谱网络 招聘 编辑:程序博客网 时间:2024/05/19 17:58
最近在做项目,用此贴记录遇到的一些小问题
1.关于json
首先需要引入3个包,我看网上有人说只需要两个,但是我没成功,引入3个之后才没问题的
关于json的返回格式,如果想增加内容的话最好用一个map集合包裹住,最近用bootstrap table做分页,要求返回的json格式如下:
也就是说要增加”total”: 792,”rows”:这些东西,后面返回的是对象集合.
{"total": 792,"rows": [{"id":1,"name":"test1","price":"$1"},{"id":2,"name":"test2","price":"$2"},{"id":3,"name":"test3","price":"$3"},{"id":4,"name":"test4","price":"$4"},{"id":5,"name":"test5","price":"$5"},{"id":6,"name":"test6","price":"$6"},{"id":7,"name":"test7","price":"$7"},{"id":8,"name":"test8","price":"$8"},{"id":9,"name":"test9","price":"$9"},{"id":10,"name":"test10","price":"$10"}]}
因此我用map包裹住
@RequestMapping(value = "/getAllUser",method = RequestMethod.POST) public @ResponseBody Map<String,Object> getAllCumUser(@RequestBody PageUtil pageUtil){ Map<String,Object> model = new HashMap<>(); //这里引入分页机制 PageHelper.startPage(pageUtil.getOffset()/pageUtil.getLimit()+1,pageUtil.getLimit()); List<CumUser> list = cumUserMapper.findAllUser(pageUtil); PageInfo<CumUser> info = new PageInfo<>(list); //这样做就可以返回上述那种格式的json了 model.put("total",info.getTotal()); model.put("rows",list); return model; }
关于接收json
可以用一个对象类来承载,在里面写上json的key这个属性值,实现set方法,spring MVC会自动注入值的
2.ajax删除用户小例子
if (confirm("确定删除该用户?")){ //必须在这里设置一下,不然传输格式有问题 $.ajaxSetup({ contentType : 'application/json' }); $.ajax( { type: 'post',//这里改为get也可以正常执行 url: '/system/cum_user_delete', contentType:"application/json;charset=utf-8", data: JSON.stringify(row), success:function (data) { if (data.delete == true){ //删除后刷新操作 $("#exampleTableToolbar2").bootstrapTable("refresh",{silent: true}); }else { alert("系统出错,删除失败,可查看tomcat日志解决") } } } ) }
对应的spring MVC控制器
@RequestMapping(value = "/cum_user_delete",method = RequestMethod.POST) public @ResponseBody Map<String,Boolean> deleteUserById(@RequestBody CumUser user){ cumUserMapper.deleteUserById(user.getId()); Map<String,Boolean> maps = new HashMap<>(); maps.put("delete",true); return maps; }
3.关于textarea乱码问题
最近写项目,表单其他控件都不乱码,就这个textarea有问题,提交方法是post,最后只能手动转码才能解决这个问题
String name= new String(problem.getPro_content().getBytes("ISO8859-1"),"UTF-8");
4.关于JSON无法解析http415错误
spring MVC提示如下
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
可能为两个原因,一个是包的问题,需要导入3个包
<dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency>
另一个原因是需要设置 content-Type为json
5.关于数据库date和java的date
java的时间类有点混乱,改了太多次了,对于这种转换,想的是在取出值使用set方法的时候进行转换,只要提前定义好我们要转换的类型,这样显示出来什么的方便多了
public void setStart_time(Date start_time) { this.start_time = sdf.format(start_time); }
6.关于单独js文件使用EL表达式的方法
1.通过设置隐藏域,付给其EL表达式的值,这样通过JS获取这个隐藏域的值即可
2.把js文件改为jsp文件,然后引入srcipt的时候其他不变,把原来的js换成jsp即可,这种做法很不错,但是编译器的js智能提示就没了
3.使用include指令引入进来,在页面直接script标签里面使用%@include引入
7.关于表单验证
使用原生的validate包,对应的注解为@Validated
使用hibernate validate验证的话,对应注解为@Valid
否则无法验证
8.关于编码过滤器不起作用
字符编码过滤器如下,不起作用的话,就是拦截有问题
<filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
可以用request.getCharacterEncoding()
判断
我的问题是,过滤器过滤不到*.do的请求,所以加一条即可
<url-pattern>*.do</url-pattern>
9.关于重定向传参数
重定向传参数可以直接拼接url传,这种方式我用EL表达式无法得到值,用request.getparames()可以得到值,也不知道为什么.
后来用了RedirectAttributes,比较好用,传参数在链接后面不显示,通过flashMap来传值的,可以用EL表达式直接访问.
RedirectAttributes attr = new RedirectAttributes(); attr.addFlashAttribute("error","layer.alert('提交错误')"); model.setViewName("redirect:/problem.do"); return model;
JS页面直接通过${error}
即可显示出来,缺点就是不可以传递对象,只能传递字符串等基本类型
10.properties文件的更新
/** * 写入properties信息,写入如果想更新的话要先load一下 */ public void writeProperties(String key, String value) { try { props = new Properties(); FileInputStream fis =new FileInputStream(fileName); props.load(fis); OutputStream fos = new FileOutputStream(fileName); props.setProperty(key, value); // 将此 Properties 表中的属性列表(键和元素对)写入输出流 props.store(fos, "『comments』Update key:" + key); } catch (IOException e) { } }
- SpringMVC学习记录(八)--开发中的小问题
- Mybatis学习记录(六)--开发中的小问题
- 记录开发过程中的一些小问题
- android学习开发中的一些问题记录
- Android 自己开发中的一些小问题记录
- ffmpeg开发中的问题(八)
- SpringMVC学习(八)——SpringMVC中的异常处理器
- css中的小问题记录
- Android开发小问题记录
- SpringMVC+Mybatis架构中的问题记录
- jQuery学习小问题记录
- APUE学习小问题记录
- 程序开发中的(小数)精度缺失问题--学习记录
- 小菜鸟学习SpringMVC(二) 注解开发
- 开发过程中的问题记录
- 记录jsp开发中的问题
- [长期记录]开发中的小知识点
- 驱动开发中的小技巧记录
- ZOJ 1831 Substitution Cypher
- Bestcoder Pro.ID 2013 蟠桃记
- 线段树+树状数组模板
- 易语言学习第六课
- C++ Standard Library
- SpringMVC学习记录(八)--开发中的小问题
- java去除重复对象
- OpenStack修改IP/MAC对VM通信影响
- strchr strrchr strcmp的实现
- 2-2 Verilog 7 段译码器(静态显示)
- sublime运行python3
- webView的页面下载实现
- HDU 2054 A==B?
- HDU2602 DP + 裸 + 01背包