Ext JS 登陆 Java权限验证 页面自动跳转
2013-05-30 13:18
766 查看
一、登陆页面设计
使用Ext JS4制作系统登陆页面代码如下:Ext.define('MyApp.view.loginform', { extend: 'Ext.form.Panel', height: 200, width: 462, renderTo:'login', bodyStyle: 'padding:20px 5px 5px 0px', iconCls: 'user', title: '物资采购管理系统', url: 'login.action',//登陆请求的struts2 具体action initComponent: function() { var me = this; Ext.applyIf(me, { items: [ { xtype: 'textfield', fieldLabel: '用户名', labelAlign: 'right', anchor: '88%', name:'username' }, { xtype: 'textfield', fieldLabel: '密码', labelAlign: 'right', anchor: '88%', name:'password' }, { xtype: 'fieldcontainer', height: 95, width: 371, layout: { type: 'absolute' }, fieldLabel: '', anchor: '80%', items: [ { xtype: 'checkboxfield', fieldLabel: '', hideLabel: false, labelAlign: 'right', labelSeparator: ' ', boxLabel: '记住密码', x: 104, y: 0 }, { xtype: 'button', handler: function(button, event) { this.up('form').getForm().submit({//提交页面 success: function(form, action) { window.location=action.result.addr;//根据返回结果重新载入页面 }, failure: function(form, action) { Ext.Msg.alert('Failed', action.result.msg);//弹出登陆错误对话框 } }); }, width: 70, iconCls: 'save', text: '确 定', x: 140, y: 30 }, { xtype: 'button', width: 70, iconCls: 'delete', text: '取 消', x: 260, y: 30 } ] } ] }); me.callParent(arguments); } });
二、业务逻辑代码
实际登陆是通过Ext JS 的AJAX方式进行的,系统的业务层使用的是Struts2框架,具体配置和代码如下:struts.xml文件中action 配置:
<action name="login" class="com.ronganpower.drawing.LoginAction"> <result type="json"/> </action>
LoginAction业务代码:
public class LoginAction { private String username; private String password; public String execute() throws IOException, JSONException{ ActionContext ctx = ActionContext.getContext(); HttpServletResponse response = (HttpServletResponse) ctx.get(ServletActionContext.HTTP_RESPONSE); HttpServletRequest servletRequest = (HttpServletRequest) ctx.get(ServletActionContext.HTTP_REQUEST); HttpSession session = servletRequest.getSession(); session.setAttribute("username", username); JSONObject jsonobj = new JSONObject(); jsonobj.put("success", true); jsonobj.put("msg", "登陆成功"); jsonobj.put("addr", session.getAttribute("addr"));//将上一页的地址发送到客户端 session.removeAttribute("addr");//删除保存的上一页地址 response.setCharacterEncoding("GB2312"); response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); out.print(jsonobj.toString()); return null; } ……
三、权限验证
web.xml中filter配置:<filter> <filter-name>LoginFilter</filter-name> <filter-class>com.ronganpower.drawing.LoginFilter</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>*.html</url-pattern> <url-pattern>*.jsp</url-pattern> <url-pattern>*.action</url-pattern> <url-pattern>*.json</url-pattern> </filter-mapping>LoginFilter类doFilter方法:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest servletRequest = (HttpServletRequest) request; HttpServletResponse servletResponse = (HttpServletResponse) response; HttpSession session = servletRequest.getSession(); String path = servletRequest.getRequestURI(); if (session.getAttribute("username") != null || path.indexOf("login") > -1) {//登录后才能访问 chain.doFilter(request, response); } else { session.setAttribute("addr", path.substring(path.lastIndexOf("/") + 1));//如果没有登陆则将访问的地址存入session备用 servletResponse.sendRedirect("/wzcg/login.html");//请求重定向到登陆页面 } }
相关文章推荐
- Javaweb---成功登陆后自动跳转jsp页面
- 实现session超时后自动跳转到登陆页面(前台JS,JAVA,判断是否Ajax请求)
- JS 控制页面超时后自动跳转到登陆页面
- 在jsp中怎么实现登录后自动跳转到登陆前浏览页面
- javascript中页面自动登陆跳转
- 织梦系统DedeCMS设定栏目及文档权限登录后跳转到登陆前的页面的实现方法
- session丢失后登陆自动跳转到 刚才操作的页面
- 当客户访问一个需登陆的页面时会转到登陆页面,当客户登陆成功后会自动跳转到客户登陆前的那个页面,请问该如何实现?
- JS实现页面超时后自动跳转到登陆页面
- session验证登陆- 页面跳转
- filter验证登录失败后为什么没有自动跳转到登录页面?
- 登陆成功后自动跳转到登录页面的前一个页面
- JAVA-JSP内置对象之response对象实现页面自动跳转
- Laravel - 已登陆用户再次查看登陆页面的自动跳转设置
- App登陆java后台处理和用户权限验证
- javaWeb项目用过滤器filter实现登陆成功后才能访问主页面,否则直接输入主页面的地址自动跳转到登陆界面
- App用户登陆 java后台处理和用户权限验证(AOP AspectJ)
- 【javaweb:前端】修改a标签的href值为javascript代码使页面不自动跳转到当前页,改成不操作
- Laravel5自动跳转到登陆前页面
- 登陆后自动跳转到登录前页面