通用页面跳转Controller

来源:互联网 发布:淘宝淘红底白字 编辑:程序博客网 时间:2024/05/22 02:02

在服务端解决jquery下ajax无法跳转页面的问题


服务端本来是全Json的,通过Rest分配URL。

原计划页面跳转都在前端完成,结果在jquery的post请求中反复尝试也无法完成页面跳转,推测是跨域的情况下浏览器拦截了。怎么办,还没去到服务端呢。

如果把方案调整成同域,那么前端就必然和后端耦合在一个工程中了。如果改成用form发HttpRequest请求,为每个跳转都写一份请求又实在麻烦。

思考可以用反射来解决大部分的参数问题,那么可以在form中加入几个跳转页面的url,在服务端统一解析,这样就可以完成通用的页面跳转了。


前面废话多,能看懂需求就好,不要在意细节偷笑改造如下

页面,里面的${}是freemarker的标签,用来写绝对地址的;{id}是活动地址,服务端会替换掉,当然也可以不用这么麻烦

<form action="${base}/redirect" method="post"><input type="hidden" name="cpath" value="${domain}/app/user/login"/><input type="hidden" name="cbsuccess" value="/{id}/home"/><input type="hidden" name="cbfail" value="/login.html"/><div class="form-group input-group"><span class="input-group-addon"  ><i class="fa fa-user" ></i></span><input type="text" name="email" class="form-control" placeholder="邮箱"></div><div class="form-group input-group"><span class="input-group-addon"  ><i class="fa fa-user" ></i></span><input type="password" name="password" class="form-control" placeholder="密码"></div><div class="form-group input-group pull-right"><a href="${base}/register.html">注册</a><button type="submit" class="button">登入</button></div></form>

服务端,CallPath类

class CallPath{private String cpath;private String cbsuccess;private String cbfail;/** * @return cpath */public String getCpath() {return cpath;}/** * @param cpath 要设置的 cpath */public void setCpath(String cpath) {this.cpath = cpath;}/** * @return cbsuccess */public String getCbsuccess() {return cbsuccess;}/** * @param cbsuccess 要设置的 cbsuccess */public void setCbsuccess(String cbsuccess) {this.cbsuccess = cbsuccess;}/** * @return cbfail */public String getCbfail() {return cbfail;}/** * @param cbfail 要设置的 cbfail */public void setCbfail(String cbfail) {this.cbfail = cbfail;}}
服务端 Controller跳转处理类
@Controllerpublic class RedirectController {public static final String CALLBACK_SUCCESS="cbsuccess";public static final String CALLBACK_FAIL="cbfail";public static final String CALL_PATH="cpath";@RequestMapping(value = "/redirect", method = RequestMethod.POST)public String redirect(CallPath cp, ModelAndView model,HttpServletRequest req){Map map = reducedLatitude(req.getParameterMap());String callpath = callpath(cp, map);return "redirect:"+callpath;}private String callpath(CallPath cp,Map map){String result=null;try{HttpRequest request = HttpRequest.post(cp.getCpath());request.form(map);request.trustAllCerts();request.trustAllHosts();JSONObject json = new JSONObject(request.body());/**这个是我在相关业务返回值中固定声明的,客官根据自己的情况修改*/if("100000".equals(json.getString("status"))){result=cp.getCbsuccess();if(result.contains("{id}")){JSONObject getResult = json.getJSONObject("result");String id = getResult.getString("_id");result = result.replace("{id}", id);System.out.println(result);}}else{result=cp.getCbfail();}}catch(Exception ex){result=cp.getCbfail();}if(StringUtils.isEmpty(result)){result="/";}return result;}private Map reducedLatitude(Map<String, String[]> map){Map<String,Object> result = new HashMap<String,Object>();for(Iterator<String> itor = map.keySet().iterator();itor.hasNext();){String key = itor.next();String[] values = map.get(key);if(values==null){continue;}else if(values.length==1){result.put(key, values[0]);}else{result.put(key, values);}}return result;}}

其中HttpRequest的源码在

https://github.com/bperin/HttpRequest/blob/master/com/github/kevinsawicki/http/HttpRequest.java


我就不横刀夺爱了,去github上面下载吧


0 0
原创粉丝点击