Spring Mvc App get - HTTP状态[404] - [未找到]

时间:2017-06-14 05:21:58

标签: java jsp spring-mvc

我使用Spring Mvc app并获取HTTP状态[404] - [未找到]。着陆页是index.jsp

enter image description here

并从控制器方法调用,

@Controller
public class BitcoinWalletController {

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

      return "index";
   }
}

index.jsp页面中,最初禁用发送资金按钮,

<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>

仅在同步完成且boolean canSendMoney返回true时才处于活动状态。

如果该按钮处于活动状态,则代码将处理提供的POST操作,

        <%--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>

下面提供了sendMoney.jsp代码,

<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>

当我输入所有必需和正确的信息并按下按钮时,它应该返回到原始页面 - index.jsp。相反,我收到错误HTTP Status [404] – [Not Found]

enter image description here

我目前无法处理地址http://localhost:8080/sendMoney.jsp。因为,如果POST提交正确,我想重定向到&#34; /&#34;。

我在项目目录的jsps文件夹中有WEB-INF

enter image description here

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

<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>

we.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>

如何解决问题?谢谢。

2 个答案:

答案 0 :(得分:1)

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

您是否在spring-config.xml文件中定义了这个?

答案 1 :(得分:1)

  

您的代码显示:您实际上是在尝试向sendMoney.jsp提交不存在的表单(可能是因为我看不到您的整个项目)。虽然您需要将表单提交给需要创建ModelAttribute的控制器。

从表单POST到控制器需要执行以下操作。

为表单字段创建一个类

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

在用HTML加载表单的GET控制器中绑定一个新的Data对象

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

以下表单将显示在您的index.jsp页面中,其中/send是控制器映射表格的位置

<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>

关注/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
    }