Po wielu badaniach nad stackoverflow, publikuję to pytanie, ponieważ nie mogłem znaleźć rozwiązania problemu.Struts2- Tag URL - Ukryj zapytanie Ciąg
Scenariusz wymagania: Zaktualizuj klienta z listy klientów na podstawie każdego identyfikatora klienta jako parametru.
Wypróbowane rozwiązanie: Na podstawie identyfikatora klienta otrzymanego z jsp należy przekazać go do tagu Działanie jako adres URL Struts2.
Problem w obliczu - Ciąg zapytania widoczny na URL.
http://foo.com/Struts2Example/getCustomerAction?customerId=2
Pytania:
- Czy nie możemy ukryć ciąg kwerendy jeśli używamy poprzeczne tag URL?
- Jeśli nie możemy ukryć ciągu zapytania przy użyciu tagu Url? jaka jest alternatywa dla powyższego scenariusza.
Kod struts.xml, JSP i działania poniżej -
<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>
Action klienta wykładowa
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;
}
Dlaczego chcesz ukryć identyfikator? Jeśli przechowujesz wartość po stronie klienta, każdy może spojrzeć na źródło i je pobrać. Możesz oczywiście użyć postu do wysłania wyniku, ale rozważ, czy użytkownik musi dodać zakładkę do strony. Naprawdę odpowiedź brzmi: bezpieczeństwo ... czy ten użytkownik powinien mieć dostęp do tego identyfikatora klienta? Jeśli nie, to nie powinno być w żadnym wypadku dozwolone. – Quaternion
Tak, użytkownik może dodać do zakładek taką stronę ... ale pytanie brzmi: "Zaktualizuj klienta", podczas gdy URL to '** getCustomer ** Action? CustomerId = 2' ... coś dziwnego tutaj:> –
@AndreaLigios - Scenariusz polega na tym, że aby zaktualizować klienta, będę musiał wstępnie wypełnić jego dane w formularzu. Aby pobrać szczegóły, przeszukuję bazę danych za pomocą identyfikatora customerId. –