0

I`m trying to get from Form parameter to a DTO object, i clicked on the button but nothing happaning, and it seems that the data isnt transfer

why is that?

Cntroller:

@RequestMapping(value = "/scanRequest", method = RequestMethod.POST)
public String scanRequest(@ModelAttribute("scanForm")UserRequestDTO userRequestDTO, BindingResult bindingResult, Model model) {

    if (bindingResult.hasErrors()) {
        return "home";
    }

    model.addAttribute("msg", userRequestDTO.getSellerName());


    return "home";
}

DTO:

public class UserRequestDTO {

    private String seller_name;

    public String getSellerName() {
        return seller_name;
    }

    public void setSellerName(String sellerName) {
        seller_name = sellerName;
    }
}

HTML Form:

<form:form method="POST" action="${contextPath}/requestlist" modelAttribute="scanForm" class="form-signin">
    <h2 class="form-signin-heading">Create your account</h2>
    <spring:bind path="seller_name">
        <div class="form-group ${status.error ? 'has-error' : ''}">
            <form:input type="text" path="seller_name" class="form-control" placeholder="Seller Name" autofocus="true"></form:input>
            <form:errors path="seller_name"></form:errors>
        </div>
    </spring:bind>

    <button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
</form:form>

Update Error:

org.apache.jasper.JasperException: javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'scanForm' available as request attribute

Update Error - 2

org.apache.jasper.JasperException: org.springframework.beans.NotReadablePropertyException: Invalid property 'seller_name' of bean class [com.searcher.model.UserRequestDTO]: Bean property 'seller_name' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

1 Answer 1

2

your post entry point is /scanRequest but your submitting to /requestlist, it fails cause you are not targeting the right mapped method .

if /requestlist is the request Mapping of your controller (the one who contains the method scanRequest) than change your form to :

<form:form method="POST" 
action="${contextPath}/requestlist/scanRequest" 
modelAttribute="scanForm"  class="form-signin">...

if is is not and your controller do

   <form:form method="POST" 
    action="${contextPath}/scanRequest" 
    modelAttribute="scanForm"  class="form-signin">...

EDIT:

Your scanForm is not present when rendering the page containing the form, so you have to add it, add this method to your controller :

@ModelAttribute("scanForm")
public UserRequestDTO getScanForm(){
  return new UserRequestDTO();
}

EDIT 2:

add a correct getter to your model :

public String getSellerName() {
        return seller_name;
    }

this is not a proper getter for attribute seller_name change it to

public String getSeller_name() {
    return seller_name;
}

or let your IDE generate them automatically.

Sign up to request clarification or add additional context in comments.

3 Comments

You right, after i change it it throw me an error (i added it to my question)
@Guyb check my edit , dude your getter is wrong, the convention is get+firtLetterto uppercase+the rest of the name of your attribute
@Guyb you are welcome , can you mark it as an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.