码迷,mamicode.com
首页 > 其他好文 > 详细

DispatchAction跳转

时间:2015-08-16 10:49:55      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:struts

一:编写界面

login.jsp
<body>
    <h1>用户登录</h1>
    <form action="/dispatchAction/loginandLoginout.do?flag=userLogin" method="post">
    u:<input type="text" name="name"/><br/>
    p:<input type="password" name="password"/><br/>
    <input type="submit" value="login"/>
    </form>
  </body>



ok.jsp
<body>
    <h1>登陆成功</h1>
    <a href="/dispatchAction/loginandLoginout.do?flag=userLogout1">退出登录方式1</a><br/>
    <a href="/dispatchAction/loginandLoginout.do?flag=userLogout2">退出登录方式2</a><br/>
</body>


二:编写userForm表单


public class UserForm extends ActionForm {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}


三:编写DispatchAction跳转方法

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.cloud.struts.form.UserForm;
public class LoginandLoginoutAction extends DispatchAction {
//响应登录请求请求
public ActionForward userLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm=(UserForm)form;
if("123".equals(userForm.getPassword())){
return mapping.findForward("loginok");
}else{
return mapping.findForward("gologin");
}

}
//该函数用于响应注销请求
public ActionForward userLogout1(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//把用户的session清空
System.out.println("userLogout1");
request.getSession().invalidate();
return mapping.findForward("gologin");
}
public ActionForward userLogout2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//把用户的session清空
System.out.println("userLogout2");
request.getSession().invalidate();
return mapping.findForward("gologin");
}
}


四:画出跳转图解

技术分享


对应的struts-config.xml代码


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans >
    <form-bean name="userForm" type="com.yourcompany.struts.form.UserForm" />
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
  <!-- parameter="flag" 用于区分用户的不同请求 -->
    <action
      attribute="userForm"
      input="/WEB-INF/ok.jsp"
      name="userForm"
      parameter="flag"
      path="/loginAndLogout"
      scope="request"
      type="com.yourcompany.struts.action.LoginAndLogoutAction"
      validate="false"
      cancellable="true">
      <forward name="loginok" path="/WEB-INF/ok.jsp" />
      <forward name="gologin" path="/WEB-INF/login.jsp" />
    </action>
  </action-mappings>
  <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
</struts-config>

版权声明:博主原创文章,转载请说明出处。http://blog.csdn.net/dzy21

DispatchAction跳转

标签:struts

原文地址:http://blog.csdn.net/dzy21/article/details/47700059

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!