new ActionForward和mapping.findForward(请求重定向和转发)
1.struts 中return new ActionForward(URI,true) 与 <forward name = "/success" path = "/page/success.jsp"/>。
1. 在ACTION里 使用
String URI = "";
return new ActionForward(URI,true);的组合,一般是因为需要在request中加入 parameter ,但如果此时在这个action里试图加入 Attribute,则转向的页面将接收不到 Attribute的值。
2. 使用return new ActionForward(mapping.findForward("/success"));
表示已经在struts的action-config.xml中配置了success的转向,如<forward name = "/success" path = "/page/success.jsp"/>。
补充:
ActionForward类提供了下面五种构造器:
public ActionForward()
public ActionForward(String path) 常用
public ActionForward(String path, boolean redirect)
public ActionForward(String name, String path, boolean redirect)
public ActionForward(String name, String path, boolean redirect, boolean contextRelative)
虽然这些构造器是不需要说明的,但我们应该注意下面几点。在这些构造器中,第二种可能是最常用的。
后四种构造器中的path参数表示的是到目的资源的路径。
后三种构造器中的redirect布尔值表示的是是否执行了一个重定向(redirect)。(缺省情况下,这个值设置为false,因为redirect比forward慢。)
最后,第五个构造器中的contextRelative布尔值表示该路径是否应该是context-relative的,而不是module-relative的。
如
return (new ActionForward("/mainMenu.jsp"));
return (new ActionForward("/login.jsp"));
分享到:
相关推荐
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { if (!ServletFileUpload.isMultipartContent(request...
ActionForward forward = mapping.findForward("success"); return forward; ``` 其中,"success"是在struts-config.xml中预先定义的forward节点名,对应要转发到的JSP页面。 ```xml <action path="/login" ...
- ActionForward:在Struts中,用于表示请求转发的目标,可以是另一个Action或者一个JSP页面。 - RequestDispatcher:Servlet API中的一个接口,用于实际的请求转发或重定向操作。 5. **跳转类型** - 请求转发...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (!ServletFileUpload.isMultipartContent(request)) ...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm = (LoginForm) form; String ...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 业务逻辑处理 boolean success = true; if ...
ActionForward定义了请求的转发路径,可以是相对路径或绝对路径。在`struts-config.xml`中,我们可以这样配置: ```xml <action path="/login" type="com.example.LoginAction" name="loginForm"> <forward name=...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { SmartUpload smartUpload = new ...
ActionForward forward = mapping.findForward("success"); response.sendRedirect(forward.getPath()); ``` 这将把用户重定向到"success"对应的页面。 2. **转发(Forward)** 转发则是服务器内部操作,不会...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 获取表单数据 String username = form....
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { try { CommonsMultipartRequestHandler handler = ...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { LoginForm loginForm = (LoginForm) form; String ...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter("username");...
public ActionForward index(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { EntryinfForm vo = (EntryinfForm) form; try { return mapping....
通过上述内容,我们可以了解到Struts框架是如何通过`ActionServlet`、`Action`、`ActionForm`、`ActionForward`和`ActionMapping`等核心组件来实现MVC设计模式的。这为开发人员提供了清晰的架构思路,有助于构建可...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException { UploadForm uploadForm = (UploadForm) form; // 假设...
- `<to>` 指定实际处理请求的路径,支持转发(forward)和重定向(redirect)两种方式。 - `$1` 表示正则表达式中第一个括号内的匹配结果,即捕获的数字。 **4. Action处理类** 创建一个名为`ProcessAction`的处理类,...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { ShoppingCart cart = (ShoppingCart) request.getSession().getAttribute(...
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // 业务逻辑处理,如从数据库获取电影信息 // ... /...
public ActionForward downloadFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String filePath = "D:\\loadfile\\temp\\yourfile....