1
votes
I am new to spring and I am facing the following issue:

Edited Description:

I was able to make a POST call by making a new controller and ModelAndView method but now I am facing one more problem of data not getting passed from JSP to controller. My JSP code is:

form:form method="POST" action="UserOperation"
        modelAttribute="DebitModel">
        <h3>Debit</h3>

                    <c:if test="${!empty listAccounts}">
                        <select name="item" id="account_dropdown"
                            onchange="changeAccountDetails(this.options[this.selectedIndex].value)">
                            <c:forEach items="${listAccounts}" var="account">
                                <option
                                    value="${account.accID},${account.acctype},${account.balance}">AccID:
                                    ${account.accID} Balance: ${account.balance}</option>
                            </c:forEach>
                        </select>
                        <br>
                        <br>
                        <label ><b>Account ID: </b></label>
                        <label id="accIdText" path="accID">${listAccounts[0].accID}</label>
                        <label><b>Account Type: </b></label>
                        <label id="accTypeText" path="acctype">${listAccounts[0].acctype}</label>
                        <label><b>Balance: </b></label>
                        <label id="accBalText" path="balance">${listAccounts[0].balance}</label>
                    </c:if>
                    <br> <br> Enter Amount<input type="text"
                        id="amount" width="80" path="amount"></input> <br> <br>
        <input type="submit" value="Debit"></input>
    </form:form>

There is a normal bean class named DebitModel. I tried to search and at many places for the data not getting passed and found solution as form:input mentioned but if I use this I get no Binding FoundException 'java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'DebitModel' available as request attribute ' even if <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> is present.

The Controller class Code:

@Controller
@RequestMapping(value = "/UserOperation")
public class DebitController {
    @Autowired
    private AccountService mAccountService;

    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView registerCustomer(@ModelAttribute("DebitModel") DebitModel debitModel, BindingResult result,
            HttpServletRequest request) throws NoSuchAlgorithmException, FileNotFoundException {

        ModelAndView modelAndView = new ModelAndView();
        System.out.println("data------------------------" + debitModel.toString());
        return modelAndView;
    }

}
2
Post that working GET method.chrylis -cautiouslyoptimistic-
This is just a suggestion can you try using (at) RequestBody instead of (at)ModelAttribute . stackoverflow.com/questions/21824012/… . @a007Albert Pinto
Show your spring mvc configuration.James Jithin

2 Answers

0
votes

show your controller class.

Seems you are using wrong pattern in path defining. Try with changing "/UserHomeDebit" to "UserHomeDebit" in both jsp and controller level.

This can check with the browser url when you are submitting the form. Lets think your application name is sampleapplication.then url will be

http://localhost:8080/sampleapplication

Then if you are use /UserHomeDebit as controller path mapping then url will change as following.

http://localhost:8080/UserHomeDebit

so its is wrong the correct one must be

http://localhost:8080/sampleapplication/UserHomeDebit

So just change the path as i said earlier and check

0
votes

In <form:form> tag's action attribute you have to specify full URL path to your application. There are several ways to do it but two of the simplest and elegant are: first by specifying an url by <c:url> tag. To do so, first you need to declare JSTL c prefix. You can do it by attaching following line at the beginning of the jsp page:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

Then to set the expected url (http://localhost:8080/[your-app-name]/UserOperation):

<c:url value="/UserOperation" var="actionUrl" />

And pass that url variableto to action attribute of the form:

<form:form method="POST" action="${actionUrl}" ...

Second solution to do this, simpler but not sure if more elegant is to get the context url of your app Spring Expression Language directly in the action attribute and attach part of url corresponding to your Controller method:

<form:form method="POST" action="${pageContext.request.contextPath}/UserOperation" ...