java 学习总结

来源:互联网 发布:淘宝机箱定制线哪家好 编辑:程序博客网 时间:2024/06/16 22:30

1.springmvc4.0 数据绑定null

springmvc4.0以上版本,如果使用tomcat,必须使用7.0以上版本,因

为,spring-webmvc.jar包需要使用到的servlet3,必须要tomcat7以上版

本才支持,如果tomcat版本在7以下,就会有诡异的问题出现,例如,

@RequestMapping(value = "/login", method = RequestMethod.POST)    public String login(String account,String password,String vcode, HttpSession session){        String redirect = REDIRECT("/login");        //基本验证        if(Strings.isNullOrEmpty(account)){            session.setAttribute("errorMsg", "账号必须填写!");            return redirect;        }        if(Strings.isNullOrEmpty(password)){            session.setAttribute("errorMsg", "密码必须填写!");            return redirect;        }        if(Strings.isNullOrEmpty(vcode)){            session.setAttribute("errorMsg", "验证码必须填写!");            return redirect;        }        //验证码        String sessionVcode = (String) session.getAttribute("vcode");        if(!vcode.equalsIgnoreCase(sessionVcode)){            session.setAttribute("errorMsg", "验证码错误!");            return redirect;        }        //验证用户名和密码        password = Cryptos.encryptMd5(password);        User loginUser = userService.login(account, password);        if(loginUser == null){            session.setAttribute("errorMsg", "账号或密码错误!");            return redirect;        }        if(loginUser.getIsLocked() == User.IsLocked.YES){            session.setAttribute("errorMsg", "账号已锁定,不能登录!");            return redirect;        }        //保存到session的时候清除密码        User currentUser = new User();        BeanUtils.copyProperties(loginUser, currentUser);        currentUser.setPassword(null);        //登录成功        session.setAttribute("currentUser", currentUser);        return REDIRECT("/admin/home");    }

我遇到就是前端post form表数据到后端,controller能解析到,

@Requestmapping也能正确定向,但是数据绑定始终显示null,使用

@QequestParam(“xxx”)显示“no present” 也是null,不知道什么原

因,使用httprequest getParamter(“xxx”)能取到值,问题最后的解

决方案是将原本tomcat6.0.43更换为tomcat7版本,springmvc数据绑定

全部正常。总结:多多注意版本的兼容!

2.spring @ResponseBody默认

spring配置中配置了以下内容,意味着默认配置了RequestMappingHandlerAdapter,并且为它默

默认配置了HttpMessageConverter

<mvc:annotation-driven/>

简而言之,只要你配置了anntation-driver并且在pom.xml中配置了默认jackson的maven依赖项,就

可以避免再配置冗长的Converter的配置,类似以下

使用fastjson作为json解析器    <mvc:annotation-driven>        <mvc:message-converters register-defaults="true">            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">                <property name="supportedMediaTypes" value="application/json"/>                <property name="features">                    <array>                        <value>WriteMapNullValue</value>                        <value>WriteDateUseDateFormat</value>                    </array>                </property>            </bean>        </mvc:message-converters>    </mvc:annotation-driven>

3.springMVC 配置CharacterEncodingFilter之后不起效果

因为tomcat配有配置编码格式,server.xml里如果不配置编码格式,则默认是iso-8859-1,加上以下内容中URIEncoding=”UTF-8”

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8"/>

4.iframe嵌套问题

session过期后,服务器销毁session导致使用iframe框架错误嵌套,解决方案如下

在登录页面中放置。

if(window.top.location.href!=location.href)        {            window.top.location.href=location.href;        }

顺便说一下,需要调试session的时候不可能等到session过期后在实验代码效果,因此需要调节

session的过期时间,调节方式是在tomcat的web.xml中设置以下内容

    <session-config>        <session-timeout>60</session-timeout>    </session-config>    //60的意思是60分钟,调试的时候设置为1,即是1分
原创粉丝点击