struts中的saveToken的作用

saveToken方法:
创建一个新的令牌,并把它保存在当前session范围内.如果HttpSession不存在,就首先创建一个HttpSession对象.

如何利用上述方法应用令牌机制解决重复提交问题:
以用户注册为例:
在用户请求newUser.jsp之前,首先把请求转发到PrepareAction,PrepareAction 调用saveToken方法,创建一个新的令牌,并将令牌值保存在当前HttpSession中(新创建的),PrepareAction接着把请求转发给newUser.jsp.

newUser.jsp中的<html:form>标签自动判断在session范围内是否存在Token,如果存在,就自动在表单中生成一个包含Token信息的隐藏字段,例如:
<input type="hidden" name="org.apache.struts.taglib.html.TOKEN" value="....">

在用户提交表单后,由InsertUserAction处理请求.在InsertUserAction中,调用isTokenValid方法,判断当前用户会话中的令牌值和请求参数中的令牌值是否匹配.如果匹配,就调用resetToken方法,删除Token,然后执行插入数据操作.如果不匹配,返回相关错误提示,进行相关操作.

OK.这样就可以有效放置重复提交了.

 

下面是一些对我自己有用的东西:

public ActionForward execute(
  ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) {
  HelloForm helloForm = (HelloForm) form;
  String username=helloForm.getUserName();
  String badUserName="monster";
  ActionError error=new ActionError("hello.dont.take.to.monster");
  ActionErrors errors=new ActionErrors();
  if((username.equalsIgnoreCase(badUserName))){
  // errors.add("username",new ActionMessage("hello.dont.take.to.monster"));
   errors.add("username",error);
   
   this.saveErrors(request, errors);
   return (new ActionForward(mapping.getInput()));
   //return (mapping.findForward("SayHello"));
  }

  // TODO Auto-generated method stub
  PersonBean pb=new PersonBean();
  pb.setUserName(username);
  pb.SaveToPersistentStore();
  request.setAttribute(Constants.PERSON_KEY, pb);
  
  return (mapping.findForward("SayHello"));

If u have declared ur resource bundle in the struts-config.xml file as given
below:
  <message-resources parameter="resources.application" />

U'll have to access this bundle in ur plain java class as follows:
MessageResources messageResources =
MessageResources.getMessageResources("resources.application");
String message = messageResources .getMessage("login.failure");

 



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=646353 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值