您好,这是我第一次在stackoverflow上发布我的问题 我是一个新手开始开发我的第一个春季mvc应用程序,当我遇到这个错误提交表格请求返回404我无法弄清楚我做错了什么,任何人都可以帮助我。
这是我的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>
我的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>
我的控制器
/*
* 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";
}
}
我的jsp页面
<!-- 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 -->
答案 0 :(得分:0)
将您的网址格式更改为*(删除/)
它变成了
<servlet-name>myblogsite</servlet-name>
<url-pattern>*</url-pattern>
检查beans xml的名称是否与Contextconfiglocation值匹配
myblogsite-servlet.xml在哪里确保它存在, 什么是servlet.xml?你是否在web.xml中配置它们
答案 1 :(得分:0)
您的视图解析程序配置不正确。检查您的jsp存在的位置。尝试删除您正在使用的前缀或使用正确的目录结构。
答案 2 :(得分:0)
我只是通过表单上的编辑和操作属性来解决有关提交表单和返回状态404的问题。
我添加此表单action =“../ admin / blog”method =“post”...... 在我的控制器上的请求映射值“admin / blog”
我第一次看起来并不是这样,但这就是我解决问题的方法。 谢谢大家回复我的问题谢谢