【发布时间】:2013-02-19 03:53:10
【问题描述】:
在对 stackoverflow 进行大量研究后,我发布了这个问题,因为我找不到该问题的解决方案。
需求场景:根据每个客户 ID 作为参数,从客户列表中更新客户。
尝试的解决方案:根据从jsp收到的客户ID,将其作为Struts2 url标签传递给Action。
面临的问题 - URL 上可见的查询字符串。
http://foo.com/Struts2Example/getCustomerAction?customerId=2
问题:
- 如果我们使用struts Url标签,我们可以不隐藏查询字符串吗?
- 如果我们在使用 Url 标签时无法隐藏 using 查询字符串?上述情况的替代方案是什么。
下面的struts.xml、jsp和动作代码-
<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 />
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>
客户操作类-
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;
}
【问题讨论】:
-
为什么要隐藏id?如果您将值存储在客户端,任何人都可以查看源代码并获取它。您当然可以使用 post 发送结果,但请考虑用户需要为页面添加书签。真正的答案是安全性……这个用户应该能够访问这个客户 ID 吗?如果不是,那么在任何情况下都不应该允许。
-
是的,用户可以为这样的页面添加书签...但是问题是
Update a customer,而URL是**getCustomer**Action?customerId=2...这里有些奇怪:> -
@AndreaLigios - 场景是,要更新客户,我必须在表单上预先填充他的详细信息。为了获取详细信息,我使用 customerId 查询数据库。
-
@Quaternion - 你问题的第一部分 - 你为什么要隐藏 id?由于查询字符串是可见的,因此用户可以重新触发该操作。所以为了避免同样的情况,我需要隐藏查询字符串。其次,由于我在这里没有使用表单,并且当我尝试将方法作为帖子提供时,流程会尝试在动作类中查找作为帖子的方法。如果我在这里错了,请告诉我。感谢您的回复。
-
如果你隐藏了URL,但用户在发送请求后多次按F5,它会发送很多相同的请求。还有其他方法(在 SO 上多次讨论)来防止这种情况......