Struts2- URL标记 - 隐藏查询字符串

P.D*_*P.D 5 tags url struts2

经过对stackoverflow的大量研究后,我发布了这个问题,因为我无法找到问题的解决方案.

要求方案:根据每个客户ID作为参数,从客户列表中更新客户.

尝试解决方案:根据从jsp收到的客户ID,将其传递给Action as Struts2 url标记.

问题Faced - 查询URL上可见的字符串.
http://foo.com/Struts2Example/getCustomerAction?customerId=2

问题:

  1. 如果我们使用struts Url标记,我们可以不隐藏查询字符串吗?
  2. 如果我们在使用Url标签时无法隐藏使用查询字符串?上述场景的替代方案是什么?

struts.xml,jsp和action的代码如下 -

<h2>All Customers Details</h2>

<s:if test="customerList.size() > 0">
    <table border="1px" cellpadding="8px">
        <tr>
            <th>Customer Id</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <th>Created Date</th>
        </tr>
        <s:iterator value="customerList" status="userStatus">
            <tr>
                <td><s:url var="editCustomer" action="getCustomerAction">
                        <s:param name="customerId" value="%{customerId}" />
                    </s:url>

                    <p>
                        <s:a href="%{editCustomer}">
                            <s:property value="customerId" />
                        </s:a>
                    </p></td>

                <td><s:property value="firstname" /></td>
                <td><s:property value="lastname" /></td>
                <td><s:property value="age" /></td>
                <td><s:date name="createdDate" format="dd/MM/yyyy" /></td>
            </tr>
        </s:iterator>
    </table>
</s:if>
<br />
<br />
Run Code Online (Sandbox Code Playgroud)

struts.xml-

<!-- Get Customer Details - To Pre-Populate the form to update a Customer -->
    <action name="getCustomerAction" method="getCustomerById"
        class="com.hcl.customer.action.CustomerAction">
        <result name="success">pages/customerForm.jsp </result>
    </action>
Run Code Online (Sandbox Code Playgroud)

客户行动课程 -

public class CustomerAction extends ActionSupport implements ModelDriven {

Logger logger = Logger.getLogger(CustomerAction.class);

Customer customer = new Customer();

List<Customer> customerList = new ArrayList<Customer>();
CustomerDAO customerDAO = new CustomerDAOImpl();

public Customer getCustomer() {
    return customer;
}

//Set Customer onto Value Stack
public void setCustomer(Customer customer) {
    this.customer = customer;
}

public List<Customer> getCustomerList() {
    return customerList;
}

//Set Customer List onto Value Stack
public void setCustomerList(List<Customer> customerList) {
    this.customerList = customerList;
}

public String execute() throws Exception {
    return SUCCESS;
}

public Object getModel() {
    return customer;
}



// Edit customer details, it will retrieve the records based on customerId
//SkipValidation is used to skip the validate()
@SkipValidation
public String getCustomerById() {

    logger.info("** Customer Id to edit ** " + customer.getCustomerId());

    customer = customerDAO.customerById(customer.getCustomerId());

    return SUCCESS;

}
Run Code Online (Sandbox Code Playgroud)

And*_*ios 1

一些无序的考虑因素:

  • 使用不同的操作(仅使用执行方法),或同一操作的不同方法,来执行不同的“操作”;
  • 每个Action/Method的名称应该与执行的操作相匹配并且不言自明,例如您应该有一个编辑客户editCustomer的方法(或Action)和一个获取客户的方法(或Action) ;getCustomer
  • 读取数据应使用GET HTTP方法,而发送数据应使用POST HTTP方法;理想情况下,每个非读取操作都应通过 POST 执行;使用 GET 发送数据是一种 20 年前诞生且从未消亡的旧习惯:/ 使用 POST 的原因是隐藏 URL、更高的负载能力、发送二进制数据的能力等...

也就是说,像这样的 URLhttp://foo.com/Struts2Example/getCustomerAction?customerId=2应该是可见的(例如添加书签),并且理想情况下应该进行美化(REST 风格,如 StackOverflow):类似http://foo.com/Struts2Example/Customer/2/

像这样的 URLhttp://foo.com/Struts2Example/editCustomerAction?customerId=2不起作用,因为您没有传递任何其他参数;您知道要编辑的客户的 ID,但不知道要更改的数据...它会变成类似: http://foo.com/Struts2Example/editCustomerAction?customerId=2&name=foo&lastname=bar&age=42,这会起作用,但正如所说(以及您问题中所询问的那样)应该隐藏,并通过 POST 处理。

source如果您在页面的 中打印IDs,则无需向用户隐藏它们;

你需要做的是确保用户不能改变ID你指定的范围之外的s; 如果您在页面中绘制了客户列表,则ID {1,2,3}必须阻止用户更改 ID 并尝试更新客户的任何尝试...要实现这一点,只需在填充页面之前ID = 4存储 ID 列表,然后根据您的列表session检查ID页面返回的 s。如果不匹配,则阻止恶意操作。

希望有帮助