• Struts 路径分析


    1.先创建 Struts_PathAction 继承至 ActionSupport ,设置execute()方法的返回值是"path"。

    public class Struts_PathAction extends ActionSupport {
    //路径问题:
    @Override
    public String execute() throws Exception {
    // TODO Auto-generated method stub
    return "path";
    }
    }

    2.配置struts.xml文件,namespace的值设置为“”,action的name与result 一致,使之操作起来更加方便。

    <struts>
    <constant name="struts.devMode" value="true" /> 
    <package name="com.cqvie.path" extends="struts-default" namespace="">
    <action name="path" class="com.cqvie.path.Struts_PathAction" >
    <result name="path">/path.jsp</result>
    </action>
    </package>
    </struts>

    3.编辑index.jsp页面,action 的name是“path”。

    <body>
    <a href="path/path.action">Action路径说明问题</a>
    </body>

    4.由上可知,需要创建path.jsp页面。

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'path.jsp' starting page</title>
    </head>
    
    <body>
    struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。
    <br />
    <a href="index.jsp">index.jsp</a>
    <br /> 虽然可以用redirect方式解决,但redirect方式并非必要。
    <br /> 解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
    <br /> 或者使用myeclipse经常用的,指定basePath
    </body>
    </html>

    注意:当这样运行时,点击超链接会出现找不到的情况,那是因为path路径有错,按理说需要加上 "../"表示根目录,但是如此一来变会更加麻烦。所以需要得到该action的根目录。

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <base href="<%=basePath%>">
    <title>My JSP 'path.jsp' starting page</title>
    </head>
    <body>

    struts2中的路径问题是根据action的路径而不是jsp路径来确定,所以尽量不要使用相对路径。<br/>
    <a href="<%=basePath%>index.jsp">index.jsp</a>
    <br />
    虽然可以用redirect方式解决,但redirect方式并非必要。
    <br />
    解决办法非常简单,统一使用绝对路径。(在jsp中用request.getContextRoot方式来拿到webapp的路径)
    <br />
    或者使用myeclipse经常用的,指定basePath
    </body>
    </html>

    再次运行时,发现可行了

    【 struts 中的Action 继承至“ActionSupport” 时,程序会重写excute()方法,且必须要有这个方法,但是是否只能执行这一个方法呢?】

    1.新建项目ActionMethod

    2.创建class ActionMethod,并继承至ActionSupport。(返回值可以直接是常量。)

    package com.struts.method;

    import com.opensymphony.xwork2.ActionSupport;

    //这种方法的返回值可以定义为常量  SUCCESS;因为 Action可以将其转换成常量:String SUCCESS = "success";

    public class ActionMethod extends ActionSupport {
    public String add(){
    return SUCCESS;//可以定义为常量。
    }
    }

    3.设置index.jsp 页面。由此验证,Action执行的时候并不一定要执行execute方法,可以在配置文件中配置Action的时候用method=来指定执行哪个方法(但是会产生太多的action);也可以在url地址中动态指定(动态方法调用DMI)(推荐)

    <a href="<%=context %>/admin/admin!add">添加用户</a>

    4.创建 struts_method.jsp,只输出一句话:提示跳转成功

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    
    <head>
    <base href="<%=basePath%>">
    </head>
    <body>
    add Admin Success!<br>
    </body>
    </html>

    5.运行从地址栏的变化可以看出动态配置 action 的method,也可实现页面跳转,并且成功,由此证明action 并不只是只能执行excute()方法。 

    6.总结:

    对struts又有了新的认识,只要每天都做一点点,我相信就会收获更多得

  • 相关阅读:
    [LeetCode] 1640. Check Array Formation Through Concatenation
    [LeetCode] 754. Reach a Number
    [LeetCode] 1457. Pseudo-Palindromic Paths in a Binary Tree
    [LeetCode] 1352. Product of the Last K Numbers
    [LeetCode] 261. Graph Valid Tree
    [LeetCode] 323. Number of Connected Components in an Undirected Graph
    [LeetCode] 1605. Find Valid Matrix Given Row and Column Sums
    [LeetCode] 1253. Reconstruct a 2-Row Binary Matrix
    [LeetCode] 455. Assign Cookies
    [LeetCode] 1358. Number of Substrings Containing All Three Characters
  • 原文地址:https://www.cnblogs.com/arriw/p/5357899.html
Copyright © 2020-2023  润新知