简体   繁体   中英

Spring mvc submit form error 404

Hi this is my first time to post my question on stackoverflow I'm a newbie starting to develop my first spring mvc app when i encounter this error submitted form request return 404 i can't figure out what i'm doing wrong can anyone please help me.

Here's my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
         xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <context-param>
        <param-name>contextClass</param-name>
        <param-value>
            org.springframework.web.context.support.AnnotationConfigWebApplicationContext
        </param-value>
    </context-param>

    <!-- Configuration locations must consist of one or more comma- or space-delimited
        fully-qualified @Configuration classes. Fully-qualified packages may also be
        specified for component-scanning -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>app.controller.*</param-value>
    </context-param>

    <display-name>MyBlogSite</display-name>
    <servlet>
        <servlet-name>myblogsite</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>myblogsite</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
     <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
        <url-pattern>*.js</url-pattern>
        <url-pattern>*.jpg</url-pattern>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/myblogsite-servlet.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/pages/404.jsp</location>
    </error-page>
</web-app>

My servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="app.controller" />

    <mvc:annotation-driven />

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- error message properties bean -->
    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
          id="messageSource">
            <property name="basename" value="messages"/>
    </bean>

</beans>

My controller

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.controller;

import dao.BlogDAO;
import javax.validation.Valid;
import models.Blog;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 *
 */
@Controller
public class BlogController {

    @RequestMapping( value="blog", method = RequestMethod.POST )
    public String blog(){
        return "blog";
    }

    @RequestMapping( value="/new", method = RequestMethod.POST )
    public String newblog( @Valid Blog blog, 
                        BindingResult result,
                         ModelMap model ){

        /* 
         * initialize blogdao
         */
        BlogDAO blogDAO = new BlogDAO();

        if( result.hasErrors() ){
            return "blog";
        }

        if( blogDAO.saveAndUpdate( blog ) ){
            model.addAttribute( "status" , true );
            model.addAttribute( "message" , "Successfully save blog." );
        }else{
            model.addAttribute( "status" , false );
            model.addAttribute( "message" , "Error saving new blog." );
        }
        return "blog";
    }
}

My jsp page

<!-- header -->
<%@include file="/admin/header.jsp" %>
<!-- end of header -->

 <!-- tinymce -->
 <script type="text/javascript" src="../tinymce/plugins/save/plugin.min.js"></script>
<script type="text/javascript">
    tinymce.init({
        selector: "#newBlog"
     });
</script>
<br/>
 <div class="jumbotron">
     <div class="container" style=" height:100px">
        <img src="../images/download.jpg"/>
    </div>
 </div>
<br/>
<form:form action="new" method="post" modelAttribute="blog" >
    <div class="row">
        <div class="col-lg-9">
           <div class="panel panel-primary">
                <div class="panel-heading">
                  <h3 class="panel-title">
                      <b>New Blog</b>
                  </h3>
                </div>
                <div class="panel-body">
                    <br/>
                       <c:if test="${status == true}">
                          <div class="alert alert-success">
                            ${message}
                          </div>
                       </c:if>
                       <c:if test="${status == false}">
                          <div class="alert alert-danger">
                            ${message}
                          </div>
                       </c:if>
                    <br/>
                        <strong>Subject:</strong>
                        <input type="text" name="subject" id="subject" placeholder="Subject" 
                               class="form-control" value="${subject}"/>
                        <form:errors path="subject" cssClass="error" />
                    <br/>
                    <textarea cols="97" rows="20" id="newBlog" name="description">
                        ${description}
                    </textarea>
                    <!-- modal -->
                    <div align="left">
                        <br/>
                        <div class="row">
                            <div class="col-sm-2"></div>
                            <div class="col-sm-2">
                            </div>
                             <div class="col-sm-5">
                                 <button id="saveblog" 
                                         class="btn btn-primary"
                                         data-target="#modalsubject">Save Blog</button>
                                 <a href="index.jsp" class="btn btn-default">Cancel</a>
                            </div>
                        </div>
                    </div>
                </div>
           </div>
        </div>
        <div class="col-lg-3">
              <div class="panel panel-info">
                <div class="panel-heading">
                  <h3 class="panel-title">Panel title</h3>
                </div>
                <div class="panel-body">
                  Panel content
                </div>
              </div>
        </div>
    </div>
</form:form>

<!-- footer -->
<%@include file="/admin/footer.jsp" %>
<!-- end of footer -->

change ur URL pattern to * (remove /)

it becomes

    <servlet-name>myblogsite</servlet-name>
    <url-pattern>*</url-pattern>

i think it helps U

check names of beans xml match with Contextconfiglocation value

where is your myblogsite-servlet.xml make sure it exists, what is servlet.xml? did you configure them in web.xml

Your view resolver is not configured properly.Check where your jsp is present. Try removing the prefix that you are using or use a proper directory structure.

I just solve my problem regarding the submited form and return status 404 by just editing and action attribute on the form.

I add this form action="../admin/blog" method="post" ...... the in my controller on the request mapping value "admin/blog"

It didn't seem to me at first time but this is how i resolve the issue. thank you to all responded back to my question thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM