Anton  Prudyus
Anton Prudyus

Reputation: 113

I can't access jsp page using Spring MVC 3

I created simple add entity form called addContact.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>

<body>
    <h2>Contact information</h2>
    <form:form method="POST" action="contacts/add" modelAttribute="contact">
        <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name" /></td>
            </tr>
            <tr>
                <td><form:label path="surname">Surname</form:label></td>
                <td><form:input path="surname" /></td>
            </tr>
            <tr>
                <td><form:label path="phonenumber">Phonenumber</form:label></td>
                <td><form:input path="phonenumber" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>
</body>

</html>

Here is rest-handler-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="facade.rest" />

    <mvc:annotation-driven />
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

Here is servlet-mapping from web.xml

<servlet-mapping>
    <servlet-name>rest-handler</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

And, finally a controller:

package facade.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Required;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.ContactDO;
import service.IContactService;


@Controller
public class ContactController {

  private IContactService contactService;


  @Required
  public void setContactService(IContactService contactService) {
    this.contactService = contactService;
  }


  @RequestMapping(value = "/contacts", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity<List<ContactDO>> getContacts() {
    List<ContactDO> contacts = contactService.load();
    if(contacts != null) {
      return new ResponseEntity<>(contacts, HttpStatus.OK);
    }
    else
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  }


  @RequestMapping(value = "/contacts/{id:.*}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public @ResponseBody ResponseEntity<ContactDO> getContact(@PathVariable Integer id) {
    ContactDO contact = contactService.loadContact(id);
    if(contact != null) {
      return new ResponseEntity<>(contact, HttpStatus.OK);
    }
    else
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
  }


  @RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
  public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
    model.addAttribute("name", contact.getName());
    model.addAttribute("surname", contact.getSurname());
    model.addAttribute("phonenumber", contact.getPhonenumber());
    //contact.setId(2);
    contactService.store(contact);
    return "contacts";
  }

}

When I am trying to access from at http://localhost:8080/pb/contacts/add (pb is a name of war) I get HTTP 400. Logs tell that I'm trying to do a get request.

Upvotes: 2

Views: 212

Answers (1)

arthur
arthur

Reputation: 3325

When I am trying to access from at http://localhost:8080/pb/contacts/add (pb is a name of war) I get HTTP 400. Logs tell that I'm trying to do a get request.

That is right !! you are trying /contacts/add using a GET request and the Request Handler can not map your request because /contacts/add is only accessible through a POST request. Look at your method definition:

@RequestMapping(value = "/contacts/add", method = RequestMethod.POST)
  public String addContact(@ModelAttribute("contact") ContactDO contact, ModelMap model) {
}

you need a POST request to reach your method.

EDIT: I suppose you want to edit a contact with your /contacts/add method. If this is the case, then please first call /contacts/{id:.*} with id being the id of you contact. and then there you can make your edit and send your post by submitting your edit. That is how you reach you /contacts/add page.

If you don't know any id, then call first http://localhost:8080/pb/contacts for your contacts. there your will (hopefully) find your contact to edit.

EDIT**: On your rest client, have you tried changing the type of request the to POST?

Upvotes: 2

Related Questions