Mvc在提交后获得HTTP状态

Mvc在提交后获得HTTP状态

本文介绍了Spring Mvc在提交后获得HTTP状态[404]-[未找到]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I work with Spring Mvc and have the controller returns the index.jsp page,

@Controller
public class BitcoinWalletController {

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

      return "index";
   }
}

这是index.jsp页面,

用于处理提交操作的代码(在index.jsp内部)

The code to handle the submission operation (inside the 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页面.

I expect that after the submission of the pop-up, I should redirected to the original index.jsp page.

相反,出现以下错误

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

应用程序结构

dispatcher-servlet.xml文件中提供的jsps位置,

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>

web.xml知道dispatcher-servlet.xml的位置,

The web.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>

除了@RequestMapping("/"),我没有其他mapping.

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

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,

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

并在sendMoney.jsp上,您可以获取放置在modelMap上的参数:

and on sendMoney.jsp you can get the parametres puted on the modelMap :

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

为获得最佳实践,请不要在jsp文件中包含Java代码

For best practices do not include java code on jsp file

这篇关于Spring Mvc在提交后获得HTTP状态[404]-[未找到]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 06:18