Dopo molte ricerche su StackOverflow, sto postando questa domanda perché non sono riuscito a trovare una soluzione per il problema.Struts2- Tag URL - Nascondi query String
Scenario di requisito: aggiornare un cliente da un elenco di clienti in base a ciascun ID cliente come parametro.
Soluzione provata: in base all'ID cliente ricevuto da jsp, passarlo all'azione come tag url Struts2.
Problema - Stringa di query visibile nell'URL.
http://foo.com/Struts2Example/getCustomerAction?customerId=2
Domande:
- Possiamo non nascondere la stringa di query se usiamo puntoni tag URL?
- Se non è possibile nascondere l'utilizzo della stringa di query durante l'utilizzo del tag Url? qual è l'alternativa per lo scenario sopra.
Codice per struts.xml, jsp e l'azione al di sotto -
<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>
Azione clienti Aula
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;
}
Perché vuoi nascondere l'id? Se si memorizza il valore sul lato client, chiunque può consultare l'origine e ottenerlo. Puoi ovviamente utilizzare post per inviare il risultato, tuttavia considera la necessità per l'utente di aggiungere un segnalibro alla pagina. La risposta è davvero la sicurezza ... questo utente dovrebbe essere in grado di accedere a questo ID cliente? In caso contrario, non dovrebbe essere consentito in nessun caso. – Quaternion
Sì, l'utente può aggiungere un segnalibro a una pagina del genere ... ma la domanda dice "Aggiorna un cliente" mentre l'URL è "** getCustomer ** Azione? CustomerId = 2' ... qualcosa di strano qui:> –
@AndreaLigios - Lo scenario è, per aggiornare un cliente, dovrò pre-popolare i suoi dettagli sul modulo. Per recuperare i dettagli, interrogo il database con il cliente. –