经过对stackoverflow的大量研究后,我发布了这个问题,因为我无法找到问题的解决方案.
要求方案:根据每个客户ID作为参数,从客户列表中更新客户.
尝试解决方案:根据从jsp收到的客户ID,将其传递给Action as Struts2 url标记.
问题Faced - 查询URL上可见的字符串.
http://foo.com/Struts2Example/getCustomerAction?customerId=2
问题:
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)
一些无序的考虑因素:
editCustomer的方法(或Action)和一个获取客户的方法(或Action) ;getCustomer也就是说,像这样的 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。如果不匹配,则阻止恶意操作。
希望有帮助
| 归档时间: |
|
| 查看次数: |
2734 次 |
| 最近记录: |