Announcement Announcement Module
Collapse
No announcement yet.
Help submitting HTML form via SpringMVC and RESTful Web Service Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help submitting HTML form via SpringMVC and RESTful Web Service

    Hello there,

    I am created an online form using SpringMVC tag library (as a JSP file) and also a Controller (think Model View Controller pattern) as a RESTful Web Service.

    When I deploy and invoke the web service by doing this in the browser:

    http://localhost:8080/myapp/applications/new

    Am able to view the SpringMVC based form (in Firefox) but when I enter in information and click on Submit, it displays the following:

    Code:
    HTTP Status 404 - /application/new
    
    type Status report
    
    message /application/new
    
    description The requested resource (/application/new) is not available.
    The RESTful Web Service (Controller):

    Code:
    @Controller
    @Path(ApplicationResource.APPLICATION_URL)
    public class ApplicationResource
    {
    	private final Logger log = LoggerFactory.getLogger(ApplicationResource.class);
    	
    	public static final String APPLICATION_URL = "/applications";
    
    	@Autowired
    	private ApplicationManager applicationManager;
    
    	@POST
    	@Path("create")
    	@Produces(MediaType.TEXT_HTML)
    	public ModelAndView getNewApplication(@Context HttpServletRequest request)
    	{
    		log.debug("Inside getNewApplication() method.");
    		ModelAndView modelAndView = new ModelAndView("/applications/applications_new");
    		String message = "";
    		try 
    		{
    			if ("POST".equalsIgnoreCase(request.getMethod()))
    			{
    				String name = request.getParameter("name");
    				String description = request.getParameter("description");
    				String imgUrl = request.getParameter("imgUrl");
    				Application app = new Application();
    				if (app != null)
    				{
    					app.setName(name);
    					app.setDescription(description);
    					app.setImgUrl(imgUrl);
    					applicationManager.save(app);
    					message = "Added application: " + app.getName();
    				}
    				else
    				{
    					message = "Invalid";
    				}
    				modelAndView.addObject("message", message);
    			}
    		}
    		catch(Exception e) 
    		{
    			log.info("Exception: ", e);
    			throw new WebApplicationException(Response.status(RestError.SERVER_ERROR_HTTP_RESP)
    					.type("application/json;charset=utf-8").entity(new ErrorOutput(RestError.SERVER_ERROR_CODE,RestError.SERVER_ERROR_MSG,e.toString())).build());
    		}
    		return modelAndView;
    	}
    
    	@GET
    	@Path("new")
    	@Produces({ MediaType.TEXT_HTML})
    	public ModelAndView getNewApplicationForm()
    	{
    		ModelAndView mv = new ModelAndView("/applications/applications_new");
    		mv.addObject("application", new Application());
    		log.debug("Inside getNewApplicationForm");
    		return mv;
    	}
    }
    applications_new.jsp (Here's the view (which does launch when I invoke it)):

    Code:
    <%@ page contentType="text/html;charset=UTF-8" language="java"%>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <html>
    <body>
    <form:form commandName="application" method="POST" action="/application/new">
    	<table>
    	<tr>
    	  <td>Name:</td>
    	  <td><form:input path="name"/></td>
    	</tr>
    	<tr>
    	  <td>Description:</td>
    	  <td><form:input path="description" /></td>
    	</tr>
    	<tr>
    	  <td>Image URL:</td>
    	  <td><form:input path="imgUrl" /></td>
    	</tr>
    	</table>
    	<input type="submit" value="submit" />
    </form:form>
    </body>
    </html>
    What could I be possibly be doing wrong?

    Thank you for taking the time to read this...

  • #2
    It's issues like this that make me wish there was a BeanPostProcessor that would scan for all @Path annotations and print out the resulting URL segments. It would be a good review of what you've declared, and comparing that against what you're trying to call would be enlightening.

    In this case, I believe you're declaring the root of your URL to be "/applications/...", but you're trying to call "/application/...".

    Comment

    Working...
    X