如何去除网站外链,wordpress advantage,怎么用flash做视频网站,wordpress keyshot提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 前言
今天来学习下#xff0c;struts1框架中实现页面跳转或请求转发的八种方式。 页面跳转方式
request的Dispatcher方法 这种方式在学习servlet编程中#xff0c;我们学… 提示文章写完后目录可以自动生成如何生成可参考右边的帮助文档 前言
今天来学习下struts1框架中实现页面跳转或请求转发的八种方式。 页面跳转方式
request的Dispatcher方法 这种方式在学习servlet编程中我们学习过适合任何项目的手动页面跳转或请求转发
action代码如下 request.getRequestDispatcher(“login.jsp”).forward(request, response); login.jsp内容可以参看我之前的文章 https://blog.csdn.net/dy051107/article/details/134743425 这种方式struts1中不推荐使用 struts1中常用的页面跳转或请求转发一般使用ActionForward类的findForward方法结合struts-config.xml文件中的action标签内的forward标签来配置下面的几种方式我们会讲到 这里作为了解 action示例代码如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class ForwardAction extends Action {Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {System.err.println(request getRequestDispatcher forward);request.getRequestDispatcher(login.jsp).forward(request, response);return null;}
}response的重定向 同样servlet中我们学习过struts1中不推荐使用作为了解 因为struts1框架中也为我们提供重定向功能下面的几种方式中我们会介绍 代码如下 response.sendRedirect(“/login.jsp”); 使用request和response的页面跳转 action的返回值是null这样就违反了struts1框架中action返回ActionForward的设计原则
action示例代码如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class ForwardAction extends Action {Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {System.err.println(response redirect);response.sendRedirect(/login.jsp);return null;}
}struts-config.xml的配置 actionpath/MultForwardLogintypexxx.xxx.action.ForwardAction/actionActionForward类的findForward方法结合struts-config.xml的配置 这种方式是struts1框架中常用的页面跳转或请求转发的方式 action中掉用ActionForward类的findForward方法进行页面跳转或请求转发 跳转的页面或请求在struts-config.xml文件中使用forward标签进行配置 struts-config.xml的配置 actionpath/MultForwardLogintypexxx.xxx.action.ForwardActionforward namesuccess path/login.jsp/forward/actionaction示例代码如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class ForwardAction extends Action {Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception {return mapping.findForward(success);}
}struts-config.xml文件中action标签的forward属性 这种方式比较简洁无需编写action类 只需在struts-config.xml文件中配置action标签的forward属性 struts-config.xml的代码如下 actionpath/MultForwardLogin forward/login.jsp/actionstruts-config.xml文件中全局跳转 这种方式多用在多个页面在某种场合下都跳转同一页面时使用 例如每个页面都需用户login后才能使用时使用全局统一配置 这样就不需要在每个action中都配置跳转同一页面 redirect默认是false页面跳转或请求转发 当设置成true时进行重定向 struts-config.xml配置代码如下 global-forwardsforward namelogin path/login.jsp redirecttrue/forward/global-forwardsActionForward类的getInputForward方法结合struts-config.xml的配置 常用在当action中发生输入验证错误或业务错误时将错误信息返回本页面时使用 例如login页面用户输入不正确的用户名和密码时错误信息显示在本页面时使用 action中调用ActionForward类的getInputForward方法跳转 在struts-config.xml文件中action标签的input属性配置跳转目标页面 actionpath/logintypexxx.xxx.action.LoginActionnameloginFormscoperequestinput/login.jspforward namesuccess path/login_success.jsp/forward/actionaction代码如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;import xxx.xxx.form.LoginForm;public class LoginAction extends Action {Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) {LoginForm lf (LoginForm) form;String username lf.getUsername();String password lf.getPassword();if (admin.equals(username) admin.equals(password)) {request.getSession().setAttribute(username, username);return mapping.findForward(success);} else {request.setAttribute(msg, 用户名和密码不正确);return mapping.getInputForward();}}
}
struts-config.xml文件中action标签的unkown属性 unkown属性也是一个全局属性当用户发送不存在的*.do的请求时 struts1框架会为我们跳转到unkown属性配置的目标页面
struts-config.xml配置代码如下 action forward/404.jsp unknowntrue/action动态跳转 根据页面输入的值动态跳转到指定页面时使用动态跳转 在action中重新new ActionForward类设定Path属性值并返回。 下边的示例是页面输入1…n跳转到page1.jsp…pagen.jsp
action代码如下
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;public class DynaForwardAction extends Action {Overridepublic ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) {ActionForward af new ActionForward();af.setPath(/page request.getParameter(page) .jsp);return af;}
}struts-config.xml文件中的action配置如下 actionpath/dyna_forwardtypexxx.xxx.action.DynaForwardAction/actionjsp代码如下
% page languagejava contentTypetext/html; charsetutf-8pageEncodingutf-8%
% taglib urihttp://struts.apache.org/tags-logic prefixlogic%
% taglib urihttp://struts.apache.org/tags-bean prefixbean%
!DOCTYPE html
html
head
meta charsetutf-8
titleInsert title here/title
/head
bodyform actiondyna_forward.do methodpostpage:input typetext name pagebrinput typesubmit valueforward/form/body
/html总结
以上就是struts1框架中的八种页面跳转或请求转发的方式总结欢迎留言补充下篇见。