struts用ajax访问的时候

plugin 发布于 2014/11/26 16:58
阅读 131
收藏 0

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

/*
	 * 用户登录
	 * 
	 * */
	public String login(){
		try {	
				FormValidate formValidate = FormValidate.getInstance();
				//1.先做格式检查,如果不对,直接返回
				
				if(!formValidate.isUsernameValid(model.getUsername()) || !formValidate.isPasswordValid(model.getPassword())){
					inputStream = new ByteArrayInputStream("用户名或密码不合法,长度在6-12位".getBytes("UTF-8"));
					return "tips"; 
				}
				//2.去数据库中查
				User user = userService.checkByUsernameAndPwd(model);	
				if(user != null){
					ActionContext.getContext().put("user", user);
					return "toForwardPage";
				}else{
					inputStream = new ByteArrayInputStream("用户名或者密码错误".getBytes("UTF-8"));
				}
			} catch (UnsupportedEncodingException e) {
				try {
					inputStream = new ByteArrayInputStream("服务器正忙,请稍后再试".getBytes("UTF-8"));
				} catch (UnsupportedEncodingException e1	) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}finally{
				if(inputStream != null){
					try {
						inputStream.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		
		return "tips"; 
		
	}



if(user != null){
					ActionContext.getContext().put("user", user);
					return "toForwardPage";

 "tips"在struts配置文件里配的是流,供ajax访问的。这一行成功的时候,我期望的结果是跳转到来时的页面,这个toForwardPage在配置文件里配了。但是结果是,ajax依然能成功,并且把toForwardPage配置的页面返回来了。新手,觉得很受伤。这个登陆注册的小功能看上去不难,做了一天了,问题百出。请指点

加载中
0
王爵nice
王爵nice
//拦截ajax请求单独处理
private boolean isAjaxRequest(HttpServletRequest request) {
 String header = request.getHeader("X-Requested-With");
 if (header != null && "XMLHttpRequest".equals(header))
 return true;
 else
 return false;
}



王爵nice
王爵nice
该评论暂时无法显示,详情咨询 QQ 群:点此入群
plugin
plugin
该评论暂时无法显示,详情咨询 QQ 群:点此入群
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部