Spring Mvc App get - HTTP Status [404] – [Not Found]

Arefe

I work with a Spring Mvc app and get HTTP Status [404] – [Not Found]. The landing page is namely index.jsp,

enter image description here

and called from the controller method,

@Controller
public class BitcoinWalletController {

   @RequestMapping("/")
   public String showBitcoinWallet() {

      return "index";
   }
}

In the index.jsp page, send money button is initially disabled,

<div class="buttons_box">
            <button type="button" class="btn btn-default btn-lg active" <%= canSendMoney ? "" : "disabled='true'"%>
                    data-toggle="modal" data-target="#myModal">Send money
            </button>
        </div>

and only be active if the synchronization is completed and boolean canSendMoney returns true.

If the button is active, the code handles the POST operation is provided,

        <%--modal contents here--%>
        <div class="modal-content">

            <div class="model-header">
                <button type="button" class="close" data-dismiss="modal">&times!</button>
                <h4 class="modal-title">Send Money</h4>
            </div>

            <form id="send-form" class="form-horizontal" action="sendMoney.jsp" method="POST">

                <div class="modal-body">

                    <div class="form-group">
                        <label for="amount" class="col-sm-2 control-label">Send</label>
                        <div class="col-xs-4">
                            <input id="amount" name="amount" class="form-control" value="0">
                        </div>
                        <div class="btc-col">
                            <span>BTC</span>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="address" class="col-sm-2 control-label">to</label>
                        <div class="col-sm-10">
                            <input id="address" name="address" class="form-control">
                        </div>
                    </div>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-default">Send</button>
                </div>

            </form>
        </div>

The sendMoney.jsp code is provided below,

<body>
<%
    String amount = request.getParameter("amount").trim();
    String address = request.getParameter("address").trim();

    WalletSendMoneyController.getSendMoneyController().send(address, amount);

    // New location to be redirected
    String site = new String("/");
    response.setStatus(response.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", site);
%>
</body>

When I put all the required and correct infos and press the button, It should return to the original page- index.jsp. Instead, I get the error, HTTP Status [404] – [Not Found],

enter image description here

I currently don't have any handle for the address http://localhost:8080/sendMoney.jsp. Because, if the POST submission is correct, I would like to redirect to the "/".

I have the jsps in the WEB-INF folder in the project directory,

enter image description here

The jsps location provided in the dispatcher-servlet.xml file,

<bean id="jspViewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsps/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

The we.xml knows where the dispatcher-servlet.xml is located,

<servlet>
        <description></description>
        <display-name>dispatcher</display-name>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

How to solve the issue? Thanks.

Zico

Your code showing: You are actually trying to submit form to sendMoney.jsp which is not exist(might be, cause i can't see your whole project). Though you need to submit the form to a controller with a ModelAttribute which you need to create.

The following things you have to do for POST to controller from form.

Make a class for form fields

public class Data {
    private String address;
    private String amount;

    public Data() {
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }
}

Bind a new Data object in GET controller where you load the form in HTML

@RequestMapping("/")
 public String showBitcoinWallet() {
    model.addAttribute("data", new Data());
    return "index";
}

The following form will be in your index.jsp page, Where /send is the controller's mapping where to POST the form

<form:form id="send-form" modelAttribute="data" class="form-horizontal" action="/send" method="POST"> 

    <div class="modal-body">

        <spring:bind path="amount">
        <div class="form-group">
            <label for="amount" class="col-sm-2 control-label">Send</label>
            <div class="col-xs-4">
                <form:input path="amount" id="amount" name="amount" class="form-control" value="0"></form:input>
            </div>
            <div class="btc-col">
                <span>BTC</span>
            </div>
        </div>
        </spring:bind>

        <spring:bind path="address">
        <div class="form-group">
            <label for="address" class="col-sm-2 control-label">to</label>
            <div class="col-sm-10">
                <form:input path="address" id="address" name="address" class="form-control"></form:input>
            </div>
        </div>
        </spring:bind>
    </div>

    <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <button type="submit" class="btn btn-default">Send</button>
    </div>

</form:form>

Following controller for /send POST

@RequestMapping(value = "/send", method = RequestMethod.POST)
    public String sendMoney(Data data) {
        //here will be your code for send money and whatever you have to do..
        ...send(data.getAddress(), data.getAmount()); 
        return "redirect:/"; //here will the location where you want to redirect
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring Mvc get HTTP Status [404] – [Not Found] after the submission

HTTP Status 404 – Not Found Spring Boot

HTTP Status 404 – Not Found tomcat spring

HTTP Status [404] ? [Not Found] for /login in spring security

How to fix "HTTP Status 404" Spring MVC

Spring MVC. HTTP Status 404

First application in Spring MVC - HTTP Status 404

Can someone explain this error for me 'HTTP Status 404 – Not Found Spring MVC'?

HTTP Status 404 - Servlet not found

Servlet: HTTP Status 404 - Not Found

HTTP Server Status 404 Error in Spring MVC Project

Spring MVC App runs in Eclipse but not Intellij - 404 Not Found

After Spring Security authentication get HTTP Status 404 error

Maven web project HTTP Status [404] – [Not Found]

Eclipse & Tomcat Error: HTTP Status 404 – Not Found:

HTTP 404 status code (Not Found) shown as 302

HTTP Status 404 – Not Found on Tomcat server

JSP/JDBC: HTTP Status 404 - Not Found

HTTP Status 404 - Not Found - Servlet and JSP

Spring type=Not Found, status=404 No message available

Spring Boot, Status 404, Error "Not Found"

HTTP Status 500 in spring MVC

Get HTTP Status 404 - Not Found, when the name of a dynamic web project renamed and deployed on Glassfish with Eclipse

Why when I submit a form into a Spring MVC application I obtain this HTTP Status 404 error page?

Redirection error in spring MVC (http status 404 the requested resource is not available jsp )

Spring MVC @Autowired and @ModelAttribute HTTP Status 404 - /WEB-INF/circle.jsp

HTTP Status 404 – Not Found: for controller - no view resolvers found in Idea

Why Spring MVC get 404?

Why does Spring MVC respond with a 404 and report "No mapping found for HTTP request with URI [...] in DispatcherServlet"?