MVC中的springmvc配置sessionn怎么了

MVC3中使用ajax的get方法获取Session为空
最近在使用ASP.Net MVC3来编写一个网站,今天在弄那个验证码模块。
在控制器中部分代码如下:
/// &summary&
/// 生成验证码图像
/// &/summary&
&returns&&/returns&
public ActionResult GetValidateCode()
&&&&&&&&&&&
ValidateCode vCode = new ValidateCode();
&&&&&&&&&&&
string code = vCode.CreateValidateCode(4);
&&&&&&&&&&&
Session["ValidateCode"] =
&&&&&&&&&&&
byte[] imBytes = vCode.CreateValidateGraphic(code);
&&&&&&&&&&&
return File(imBytes, @"image/jpeg");
/// &summary&
/// 获取当前验证码
/// &/summary&
&returns&&/returns&
//[HttpPost]
public string GetCurrentValidateCode()
&&&&&&&&&&&
string str = Session["ValidateCode"].ToString();
&&&&&&&&&&&
View中的部分脚本代码如下:
//////////////////////////////////////首次加载验证码图片
$("#ImCode").attr("src", "/QueryBusShift/GetValidateCode?time=" +
(new Date().getTime()));
$.get("/QueryBusShift/GetCurrentValidateCode", function (data)
&&&&&&&&&&&
$("#ValidateCode").val(data);
&&&&&&&&&&&
alert($("#ValidateCode").val());
//////////////////////////////////////绑定单击验证码图片的事件
$("#ImCode").bind("click", "", function () {
&&&&&&&&&&&
this.src = "/QueryBusShift/GetValidateCode?time=" + (new
Date().getTime());
&&&&&&&&&&&
$.get("/QueryBusShift/GetCurrentValidateCode", function (data)
&&&&&&&&&&&&&&&
$("#ValidateCode").val(data);
&&&&&&&&&&&&&&&
alert($("#ValidateCode").val());
&&&&&&&&&&&
但是我的对应隐藏字段的值和验证码图片中的值始终无法对应,隐藏字段中的值始终是第一次打开页面时候的验证码的字符串,点击验证码图片会更换验证码图片,而隐藏字段的值始终不变。
调试了很久,还是一样,发现好像更换验证码图片的时候session的id发生了变化,设置断点调试的时候发现后台提交请求的时候相应的action部分没有进入,在网上找了很久资料,尝试了一些解决办法,但是还是没有效果,突然想到可能是ajax提交时候的问题,带着试一试的想法,我将上面的ajax的get方法改成了post方法,居然成功了,但是还是不大清楚为什么会这个样子,估计是ajax提交那部分所写的代码有问题。
最终view部分的相关代码如下:
//////////////////////////////////////首次加载验证码图片
$("#ImCode").attr("src", "/QueryBusShift/GetValidateCode?time=" +
(new Date().getTime()));
$.post("/QueryBusShift/GetCurrentValidateCode", {},
function (data) {
&&&&&&&&&&&
$("#ValidateCode").val(data);
&&&&&&&&&&&
alert($("#ValidateCode").val());
}, "text");
//////////////////////////////////////绑定单击验证码图片的事件
$("#ImCode").bind("click", "", function () {
&&&&&&&&&&&
this.src = "/QueryBusShift/GetValidateCode?time=" + (new
Date().getTime());
&&&&&&&&&&&
$.post("/QueryBusShift/GetCurrentValidateCode", {},
function (data) {
&&&&&&&&&&&&&&&
$("#ValidateCode").val(data);
&&&&&&&&&&&&&&&
alert($("#ValidateCode").val());
&&&&&&&&&&&
}, "text");
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。【springMVC】controller中获取session - 推酷
【springMVC】controller中获取session
平时使用springMVC
在方法中访问session中经常很自然地调用Servlet API。
用起来非常直观方便,一直没有多考虑什么。
比如这样:
@RequestMapping(value = &/logout&)
public String logout(HttpSession session) {
session.removeAttribute(&user&);
return &/login&;
但毕竟这样对Servlet API产生了依赖,感觉不够pojo。
于是我试着解决这个问题。
我打算用一个注解,名字就叫&sessionScope&,Target可以是一个Method,也可以是Parameter。
也就是说:
import java.lang.annotation.D
import java.lang.annotation.ElementT
import java.lang.annotation.R
import java.lang.annotation.RetentionP
import java.lang.annotation.T
@Target({ ElementType.PARAMETER,ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SessionScope {
String value();
然后我要注册一个ArgumentResolver,专门解决被注解的东东,把他们统统替换成session里的东西。
代码如下:
import mons.lang3.StringU
import org.slf4j.L
import org.slf4j.LoggerF
import org.springframework.core.MethodP
import org.springframework.web.bind.support.WebDataBinderF
import org.springframework.web.context.request.NativeWebR
import org.springframework.web.context.request.RequestA
import org.springframework.web.method.support.HandlerMethodArgumentR
import org.springframework.web.method.support.ModelAndViewC
public class SessionScopeMethodArgumentResolver implements
HandlerMethodArgumentResolver {
public boolean supportsParameter(MethodParameter parameter) {
if(parameter.hasParameterAnnotation(SessionScope.class))
else if (parameter.getMethodAnnotation(SessionScope.class) != null)
public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
String annoVal =
if(parameter.getParameterAnnotation(SessionScope.class)!=null){
logger.debug(&param anno val::::&+parameter.getParameterAnnotation(SessionScope.class).value());
annoVal = parameter.getParameterAnnotation(SessionScope.class).value();
}else if(parameter.getMethodAnnotation(SessionScope.class)!=null){
logger.debug(&method anno val::::&+parameter.getMethodAnnotation(SessionScope.class).value());
annoVal = parameter.getMethodAnnotation(SessionScope.class)!=null?StringUtils.defaultString(parameter.getMethodAnnotation(SessionScope.class).value()):StringUtils.EMPTY;
if (webRequest.getAttribute(annoVal,RequestAttributes.SCOPE_SESSION) != null){
return webRequest.getAttribute(annoVal,RequestAttributes.SCOPE_SESSION);
final Logger logger = LoggerFactory.getLogger(SessionScopeMethodArgumentResolver.class);
supportParameter判断对象是否被注解,被注解则进行resolve。
resolve时获取注解值,注解值为session key,用webRequest从session scope中取值。
另外需要将此配置放到org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter的customArgumentResolvers列表中,可以使用bean标签配置,也可以直接使用mvc标签。
namespace为:xmlns:mvc=&
schema location为:http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
&mvc:annotation-driven&
&mvc:argument-resolvers&
&bean class=&mon.SessionScopeMethodArgumentResolver& /&
&/mvc:argument-resolvers&
&/mvc:annotation-driven&
&mvc:default-servlet-handler /&
话说mvc:annotation-driven和mvc:default-servlet-handler的顺序不能颠倒,该不会只有我出现这种情况吧- -..
现在可以在controller中使用了,比如:
@RequestMapping(value = &/index&)
@SessionScope(&currentUser&)
public ModelAndView index(User currentUser) {
if (currentUser==null || currentUser.getId()==null)
mav = new ModelAndView(&/login&);
mav = new ModelAndView(&/index&);
或者在参数上注解:
@RequestMapping(value = &/welcome&)
public String welcome(@SessionScope(&currentUser&)User currentUser) {
return &/main&;
至于更新session,目前只是用@sessionAttributes配合ModelMap的方式。
嗯,我不是很喜欢这种方式...
或者我可以不用这样做,直接集成Apache Shiro,在controller中直接getSubject()。
把用户信息完全让shiro负责,嗯,这个好。
已发表评论数()
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见
正文不准确
标题不准确
排版有问题
主题不准确
没有分页内容
图片无法显示
视频无法显示
与原文不一致2013年 总版技术专家分年内排行榜第一
2014年 总版技术专家分年内排行榜第三
本帖子已过去太久远了,不再提供回复功能。Mvc Session为什么丢失?_最火下载站
您的位置: >
> Mvc Session为什么丢失?
Mvc Session为什么丢失?
环境:mvc 1.0 问题:在系统启动时会产生一个Session. Session[&admin_ID&] = &aa27ec10-d9d4-43d9-96f6-7cfb0d5099ca&; 但是在执行了下面代码后,Session就会丢失。 C# code [AcceptVerbs(HttpVerbs.Post)] public ActionResult GenerateVerifyModel(FormCollection formCollection) { ... return File(zipPath, &application/x-zip-compressed&); } 百思不得其解,为什么下载文件后就会丢失session? 后来在Web.config中进行了如何设置: &sessionState mode=&StateServer& cookieless=&false& timeout=&60& stateConnectionString=&tcpip=127.0.0.1:42424& stateNetworkTimeout=&3600& /& 但是运行时提示: Unable to serialize the session state. Please note that non-serializable objects or MarshalByRef objects are not permitted when session state mode is 'StateServer' or 'SQLServer'. 看这个提示应该是说Session中的内容无法序列化,可是Session中只有一个GUID啊。Session[&admin_ID&] = &aa27ec10-d9d4-43d9-96f6-7cfb0d5099ca&;
上一篇: 下一篇:}

我要回帖

更多关于 mvc中使用session 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信