I work with a Spring Mvc app
and get HTTP Status [404] – [Not Found]. The landing page is namely index.jsp
,
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">×!</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]
,
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,
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.
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 aModelAttribute
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.
Comments