Spring Mvc在提交后获取HTTP状态[404] - [未找到]

时间:2017-06-14 08:15:16

标签: java jsp spring-mvc

我使用Spring Mvc并让控制器返回index.jsp页面,

@Controller
public class BitcoinWalletController {

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

      return "index";
   }
}

这是index.jsp页面,

enter image description here

处理提交操作的代码(在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>

提供的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>

我希望在提交弹出窗口后,我应该重定向到原来的index.jsp页面。

enter image description here

相反,我收到以下错误,

HTTP Status [404] – [Not Found] after the submission

enter image description here

应用结构,

enter image description here

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>

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>

我没有其他mapping,而是@RequestMapping("/")

如果我从action="sendMoney.jsp"中删除了index.jsp,并按照建议使用POST方法控制另一个控制器,则会出现以下错误:

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)

如何解决问题?感谢。

1 个答案:

答案 0 :(得分:1)

请尝试处理来自控制器的发布请求

@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);
   }

并且在sendMoney.jsp上你可以获得在modelMap上的参数:

<label>Amount</label> <span>${amount}</span>
<label>Adress</label> <span>${address}</span>

对于最佳实践,请不要在jsp文件中包含java代码