NutZ内置了一个CheckSession类,可以用来检查session变量是否存在,不存在跳转到指定的页面。
现在,想实现,登录后,跳转到之前请求的地址。
先看下CheckSession.java的代码
package org.nutz.mvc.filter;
import org.nutz.mvc.ActionContext; import org.nutz.mvc.ActionFilter; import org.nutz.mvc.Mvcs; import org.nutz.mvc.View; import org.nutz.mvc.view.ServerRedirectView;
public class CheckSession implements ActionFilter {
private String name; private String path;
public CheckSession(String name, String path) { this.name = name; this.path = path; }
public View match(ActionContext context) { Object obj = Mvcs.getHttpSession().getAttribute(name); if (null == obj) return new ServerRedirectView(path); return null; }
}
照葫芦画瓢,新建一个类,也继承ActionFilter即可。
import org.nutz.mvc.ActionContext; import org.nutz.mvc.ActionFilter; import org.nutz.mvc.Mvcs; import org.nutz.mvc.View; import org.nutz.mvc.view.ServerRedirectView;
public class CheckWebLogin implements ActionFilter {
private String name; private String path;
public CheckWebLogin(String name, String path) { this.name = name; this.path = path; }
public View match(ActionContext context) {
Object obj = Mvcs.getHttpSession().getAttribute(name); if (null == obj) { // 返回的地址 String toURL = null;
// 参数串 String queryStr = Mvcs.getReq().getQueryString(); if (queryStr != null && queryStr.length() > 0) { queryStr = queryStr.replaceAll("&", "^_^"); toURL = Mvcs.getReq().getRequestURI() + "?" + queryStr; } else { toURL = Mvcs.getReq().getRequestURI(); }
// Ajax访问的,返回json数据,由客户端跳转。 String requestWith = Mvcs.getReq().getHeader("X-Requested-With"); if (requestWith != null && requestWith.equals("XMLHttpRequest")) { Map<String, String> m = new HashMap<String, String>(); m.put("result", "LOGIN"); m.put("msg", "请登录后再进行此操作。"); return new ViewWrapper(new UTF8JsonView(JsonFormat.nice()), m); }
return new ServerRedirectView(path + "?back=" + toURL); } return null; }
}
从request里得到请求地址,拼接到地址里。
也就是把参数传递给了login页面,那么在toLogin的方法里,注意接收参数, 然后jsp页面写入到登录表单的隐藏框中,最终传递给login方法。
@IocBean @Filters(@By(type = CheckWebLogin.class, args = { "admin", "login" })) public class SeaAreaAction extends BaseAction { .... @At("/seaArea/login") @Ok("jsp:seaArea.login") @Filters public void toLogin(@Param("back") String back, HttpServletRequest request) {
// 回传表单输入 Map<String, String> itemMap = new HashMap<String, String>(); itemMap.put("username", ""); itemMap.put("password", ""); itemMap.put("back", back); request.setAttribute("formdata", itemMap); }
@At("/seaArea/login") @Fail("jsp:seaArea.login") @POST @Filters public View doLogin(@Param("username") String userName, @Param("password") String password, @Param("back") String back, HttpSession session, HttpServletResponse response, HttpServletRequest req) {
// 回传表单输入 Map<String, String> itemMap = new HashMap<String, String>(); itemMap.put("username", userName); itemMap.put("password", password); itemMap.put("back", back); req.setAttribute("formdata", itemMap);
// 检查用户 ......
// 有返回地址的,跳转过去 if (back != null && back.length() > 0) { // back地址带工程名,要去掉 back = back.substring(request.getContextPath().length()); back = back.replaceAll("\\^_\\^", "&"); // ^_^替换回& return new ServerRedirectView(back); } else { // 其它跳到默认页面 return new ServerRedirectView("/"); }
}
@At("/seaArea/logout") @Ok(">>:/seaArea/login") @Filters public void toLogout(HttpSession session) { session.removeAttribute("admin"); } }
|