Restlet combat (7) - submit and process Web Form

This section demonstrates how to use Restlet to create a Customer by submitting a Web Form.

 

First create a customer.jsp as a test form submission file

Java code   Favorite code
  1. <html>  
  2. <head>  
  3.   
  4. <script>  
  5.     function doSubmit(){  
  6.         document.forms[0].submit();  
  7.     }  
  8. </script>  
  9.   
  10. </head>  
  11.   
  12. <body>  
  13.   
  14. <form name="form1" action="<%=request.getContextPath()%>/resources/customers" method="post">  
  15.     Name:<input type="text" name="name"/><br>  
  16.     Address:<input type="text" name="address"/><br>  
  17.     <input type="button" value="Submit" onclick="doSubmit()"/>  
  18. </form>  
  19. </body>  
  20. </html>  

 

As mentioned in the previous article , if you want to create a Customer, the URL should be: /customers, and the corresponding resource is CustomersResources.

 

CustomersResources already has the code for receiving Form submission, so no modification is required, see the previous article.

 

Let's test this function, start the Web server, run customer.jsp, fill in the two fields of Name and Address on the displayed page, and then submit the current form, an error page appears, and the error message is as follows:




 
 This error is obvious, because we specify method="post" in the definition of form, so the method corresponding to Post in CustomersResource should be called, but there is no such method in our program, so should we specify method="put" "Woolen cloth? Because put corresponds to modification, and post corresponds to the concept of creation. But it is obviously not possible, because the method in the form only supports "get" and "post". Therefore, if the request is submitted from the Form, whether it is modified or created, it will only access the acceptRepresentation method corresponding to Post. .

 

If the definition of the form itself does not support it, then we can only define the method corresponding to Post in the CustomersResource to act as the creation method. The added code is as follows:

 

 

Java code   Favorite code
  1. @Override  
  2. public boolean allowPost() {  
  3.     return true;  
  4. }  
  5.   
  6. @Override  
  7.    public void acceptRepresentation(Representation entity) throws ResourceException {  
  8.     Form form = new Form(entity);  
  9.     Customer customer = new Customer();  
  10.     customer.setName(form.getFirstValue("name"));  
  11.     customer.setAddress(form.getFirstValue("address"));  
  12.     customer.setRemarks("This is an example which receives request with put method and save data");  
  13.       
  14.     customerDAO.saveCustomer(customer);  
  15. }  

 

再次测试,yes, It is ok.

 

 

BTW: When it comes to Form, it only supports two methods: get and post. For Cetia4, another framework that supports Rest, since the custom form tag cetia:form is used, in addition to the get and post parameters, you can also specify Specific method names, such as:

 

Java code   Favorite code
  1. <cetia:form method="insertCustomer" action="tasks">  

 

 If this is the case, when Cetia4 parses the tag, it will think that the method of the currently submitted form is post, but the specific method in the class is insertCustomer.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326472083&siteId=291194637