Struts2自定义拦截器,实现发表文章检查是否已登陆功能
来源:互联网 发布:linux svn 分支 编辑:程序博客网 时间:2024/06/01 18:29
老师布置了个份作业:要求用Struts2实现定义一个拦截器,如果用户发表评论,就检查是否用户已经登陆,如果已经登陆,正常发表,如果没有登陆,则跳转到登陆页面。这个功能很简单,就当做复习拦截器吧。
jsp页面:
然后是action:
CommendAction代码如下:
package com.lin.action;import java.util.Map;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class CommentAction extends ActionSupport {private String word ;public String getWord() {return word;}public void setWord(String word) {this.word = word;}public String execute(){ActionContext cx = ActionContext.getContext();cx.put("word",word);System.out.println("Comment is running");return "commend_SUCCESS";}}
LoginAction代码如下:
package com.lin.action;import java.util.Map;import com.lin.model.User;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class LoginAction extends ActionSupport{private User user ;public User getUser() {return user;}public void setUser(User user) {this.user = user;}public String execute(){ActionContext ctx = null;ctx = ActionContext.getContext();Map<String,Object> session = ctx.getSession();if( "admin".equals(user.getUsername()) && "pass".equals(user.getPassword())){session.put("User",user);return "login_SUCCESS";}else{session.put("User", null);return "login_FALSE";}}}
CommentInterception代码如下:
package com.lin.interception;import java.util.Map;import com.lin.action.CommentAction;import com.lin.model.User;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;public class CommentInterception extends AbstractInterceptor{@Overridepublic String intercept(ActionInvocation invocation) throws Exception {CommentAction commentAction = (CommentAction) invocation.getAction();ActionContext ctx = null;ctx = ActionContext.getContext();Map<String,Object> session = ctx.getSession();User user = (User) session.get("User");if(user == null){return "commend_FALSE";}else{return invocation.invoke();}}}
Struts.xm代码如下:
<?xml version="1.0" encoding= "UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <package name ="TestOne" extends="struts-default" namespace="/"> <interceptors><interceptor name="myinterceptor" class="com.lin.interception.CommentInterception"></interceptor></interceptors> <action name="comment" class="com.lin.action.CommentAction"> <result name="commend_SUCCESS">/jsp/result/commend_SUCCESS.jsp</result> <result name="commend_FALSE">/jsp/login.jsp</result> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="myinterceptor"></interceptor-ref> </action> <action name="login" class="com.lin.action.LoginAction"> <result name="login_SUCCESS">/jsp/index.jsp</result> <result name="login_FALSE">/jsp/result/login_FALSE.jsp</result> </action> </package> </struts>
User类代码如下:
package com.lin.model;public class User {private String username = null;private String password = null;public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
0 0
- Struts2自定义拦截器,实现发表文章检查是否已登陆功能
- Struts2自定义拦截器,实现登录检查。
- Struts2自定义拦截器,检查用户是否登录
- Struts2 自定义拦截器 实现简单权限检查
- struts2自定义拦截器做登陆模块
- 用struts2拦截器实现权限检查
- 用struts2拦截器实现权限检查
- struts2 拦截器实现登陆验证示例
- struts2定义拦截器(Interceptor)检查用户是否已经登录
- Struts2 登陆拦截器
- struts2登陆拦截器
- Struts2 拦截器的使用,验证是否已经登陆
- 登陆检查设置拦截器
- 20171106_chr_downInterceptor 自定义拦截器实现下载前登陆拦截
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器实例—登陆权限验证
- Struts2自定义拦截器实例—登陆权限验证 .
- Struts2自定义拦截器实例—登陆权限验证
- 设计模式之原型模式
- 【Solr4.7.0学习笔记】系列之一:如何让solr和java程序整合
- Ural 1519 Formula 1 插头DP
- 如何在JavaWeb程序中使用tld文件
- Java基本数据类型及之间的转换
- Struts2自定义拦截器,实现发表文章检查是否已登陆功能
- tag标签文件
- 冒泡排序
- JSP usebean
- python 小型的刷博器
- CodeForces 21C Stripe 2 (简单题)
- OpenSSH免密码登录SSH2
- oracle 外键
- 【坐在马桶上看算法】算法6:只有五行的Floyd最短路算法