[英]Spring Mvc get HTTP Status [404] – [Not Found] after the submission
I work with Spring Mvc and have the controller returns the index.jsp
page, 我使用Spring Mvc,并让控制器返回
index.jsp
页面,
@Controller
public class BitcoinWalletController {
@RequestMapping("/")
public String showBitcoinWallet() {
return "index";
}
}
This is the index.jsp
page, 这是
index.jsp
页面,
The code to handle the submission operation (inside the index.jsp
), 处理提交操作的代码(在
index.jsp
内部),
<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>
The code for the sendMoney.jsp
provided, 提供的
sendMoney.jsp
的代码,
<%@ page import="com.puut.bitcoin.WalletSendMoneyController" %>
<html>
<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>
</html>
I expect that after the submission of the pop-up, I should redirected to the original index.jsp
page. 我希望在提交弹出窗口之后,我应该重定向到原始的
index.jsp
页面。
Instead, I get the following error, 相反,出现以下错误,
HTTP Status [404] – [Not Found] after the submission
The app structure, 应用结构
The jsps location provided in the dispatcher-servlet.xml file, 在dispatcher-servlet.xml文件中提供的jsps位置,
<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 web.xml knows where the dispatcher-servlet.xml is located, web.xml知道dispatcher-servlet.xml的位置,
<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>
I have no other mapping
then the @RequestMapping("/")
. 我没有其他
mapping
然后@RequestMapping("/")
。
If I remove the action="sendMoney.jsp"
from the index.jsp
and have another controller with the POST
method as suggested, I get the following stack of errors, 如果我从
index.jsp
删除action="sendMoney.jsp"
,并按照建议的方法使用了另一个具有POST
方法的控制器, action="sendMoney.jsp"
收到以下错误堆栈,
Type Exception Report
Message Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.http.MediaType.compareTo(Lorg/springframework/http/MediaType;)I
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.NoSuchMethodError: org.springframework.http.MediaType.compareTo(Lorg/springframework/http/MediaType;)I
org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletionWithError(DispatcherServlet.java:1259)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)
org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838)
javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
How to solve the issue? 该如何解决呢? Thanks.
谢谢。
Please to try handle the Post Request from controller 请尝试处理控制器发出的过帐请求
@RequestMapping(value="/", method=RequestMethod.POST)
public ModelAndView PostBitcoinWallet(@RequestParam String amount,@RequestParam String address) {
ModelMap modelMap = new ModelMap();
modelMap.put("amount", amount);
modelMap.put("address", address);
return new ModelAndView("sendMoney",modelMap);
}
and on sendMoney.jsp you can get the parametres puted on the modelMap : 在sendMoney.jsp上,您可以获取放置在modelMap上的参数:
<label>Amount</label> <span>${amount}</span>
<label>Adress</label> <span>${address}</span>
For best practices do not include java code on jsp file 为了最佳实践,请不要在jsp文件中包含Java代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.