struts2 动态调用action中方法

1. jsp页面代码

submit 提交的action 写成"action_login" ,和sturts2文件中配置的name=“action_*” 匹配, " * "通配符在将此作为了sturts2文件里的{1} 的参数值,即login 。

<s:form action="admin_login">
     <table border="0" cellpadding="0" cellspacing="0" align="center" width="300" height="200" style="margin:auto;">
                        <tr>
                            <td width="100"><s:text name="login_label_loginname"/>:</td>
                            <td><s:textfield name="loginName" size="26"/></td>
                        </tr>
                        <tr>
                            <td width="100"><s:text name="login_label_password"/>:</td>
                            <td><s:password name="loginPwd" size="26"/></td>
                        </tr>
                        <tr>
                            <td width="100"><s:text name="login_label_rand"/>:</td>
                            <td><s:textfield name="rand" size="26"/>&nbsp;
                            <img name="pic" id="pic" src="<%=basepath%>/common/rand.jsp" height="16" border="1" onClick="changeRand();" style="cursor:pointer;" alt="点击重新获取验证码"/></td>
                        </tr>
                        <tr>
                            <td colspan="2" align="center">
                                <s:submit key="login_submit"/>
                            </td>
                        </tr>
     </table>
     </s:form>

 

2. sturts2文件中的配置

<action name="admin_*" class="adminAction" method="{1}">
   <result>/admin/{1}.jsp</result>
   <result name="input">/admin/{1}.jsp</result>   
   <result name="index">/admin/index.jsp</result>
   <result name="login">/admin/login.jsp</result>
   <result name="toBrowseAdmin" type="redirect-action">
    <param name="actionName">admin_browseAdmin</param>
    <param name="namespace">/admin</param>
    <param name="actionMsg">${actionMsg}</param>
   </result>
   <interceptor-ref name="loginedCheck"/>
   <interceptor-ref name="defaultStack"/>
  </action>

 

3. action 文件中的方法

//获取login.jsp界面 中输入的账户和密码

private Admin model = new Admin();

 public Admin getModel()
 {
  return model;
 }

//通过sturts2文件中配置的路径调用action中的登录方法

public String login()
 {
  String strReturn = "login";
   if (1!=1 && !rand.equalsIgnoreCase((String) ServletActionContext.getRequest().getSession().getAttribute("rand")) )
  {
    addActionError(getText("login_rand_error"));
  }
  else
  {
   System.out.println("1233");
    Admin tAdmin = ((AdminService) service).getAdminByNameAndPwd(model.getLoginName(), model.getLoginPwd());
   if ( tAdmin != null )
   {
    ServletActionContext.getRequest().getSession().setAttribute("admin", tAdmin);
    strReturn = "index"; // ��½У��ɹ�����ת��������ҳ
   }
   else
   {
    addActionError(getText("login_fail"));
   }
  }

  return strReturn;
 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值