导致spring mvc出现404的一种情况

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/baidu_30809315/article/details/78110532

原因:web配置文件引入了两个配置文件,一个是spring的,一个是spring mvc的,spring配置文件下配置了包扫描,而spring mvc配置文件没有配置,在spring mvc上填上就好了

  <!-- Spring 配置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
      classpath*:spring/spring-context.xml
    </param-value>
  </context-param>

  <!-- spring mvc 配置 -->
  <servlet>
    <description>spring mvc servlet</description>
    <servlet-name>springMvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <description>spring mvc 配置文件</description>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        classpath*:spring/spring-mvc.xml
      </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

spring-context.xml中配置的包扫描

    <!-- 配置要扫描的包 -->
    <context:component-scan base-package="com.example">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

spring-mvc.xml中配置的包扫描

    <!-- 自动扫描controller包下的所有类,使其认为spring mvc的控制器 -->
    <context:component-scan base-package="com.example">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Controller" />
        <context:include-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice" />
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Service" />
    </context:component-scan>


展开阅读全文

spring mvc 404

02-02

开始的时候还好好的,到后面加了点东西就变成404错误了,求教。rnweb.xmlrn[code=java]rnrnrn rn defaultrn /js/*rn /css/*rn /images/*rn /fonts/*rnrn rnrn encodingFilterrn org.springframework.web.filter.CharacterEncodingFilterrn rn encodingrn UTF-8rn rnrnrn rn encodingFilterrn /*rn rnrn rn rn rn rn rn rn rn rnrn rn rn rn rnrn rn rn rn rnrn rn rn rn rn rnrn rn rn rn rnrn rnrn rnrn rnrn rnrn rnrn rnrn rnrn rnrn rnrn rn LoginServletrn ppdai.user.web.servlet.LoginServletrn rn rn VerifyCodeServletrn ppdai.user.web.servlet.VerifyCodeServletrn rn rn RegistServletrn ppdai.user.web.servlet.RegistServletrn rnrnrnrn rn LoginServletrn /LoginServletrn rn rn VerifyCodeServletrn /VerifyCodeServletrn rn rn RegistServletrn /RegistServletrn rnrn rn rn springMybatisrn org.springframework.web.servlet.DispatcherServletrn 1rn rn rn springMybatisrn /WEB-INF/config/*-servlet.xmlrn rn rn springMybatisrn /views/*rn rnrn rn springMybatisrn /jsps/*rn rnrn rn rn rn rnrn rn contextConfigLocationrn /WEB-INF/config/spring-*.xmlrn rnrn rn org.springframework.web.context.ContextLoaderListenerrn rn rn DruidStatViewrn com.alibaba.druid.support.http.StatViewServletrn rn rn DruidStatViewrn /druid/*rn rnrnrnrnrn rnArchetype Created Web Applicationrnrn[/code]rnrnControllerrn[code=java]package ppdai.user.web.controller;rnrnimport org.springframework.stereotype.Controller;rnimport org.springframework.web.bind.annotation.RequestBody;rnimport org.springframework.web.bind.annotation.RequestMapping;rnimport org.springframework.web.bind.annotation.RequestMethod;rnimport org.springframework.web.bind.annotation.ResponseBody;rnimport org.springframework.web.servlet.ModelAndView;rnrnimport javax.servlet.ServletContext;rnimport javax.servlet.http.HttpServletRequest;rnimport javax.servlet.http.HttpServletResponse;rnimport javax.servlet.http.HttpSession;rnimport java.util.HashSet;rnimport java.util.Set;rnrn/**rn * Created by yangliwen on 2017/1/23.rn */rnrn@Controllerrn@RequestMapping(value = "/jsps")rnpublic class HomeController rnrn @RequestMapping(value = "/index.html",method = RequestMethod.GET)rn public ModelAndView Index(HttpServletRequest request,HttpServletResponse response)rn rn Object object= request.getSession().getAttribute("username");rn if(object!=null)rn rn ServletContext context=request.getServletContext();rn context.setAttribute("Username",object.toString());rn context.setAttribute("HasSign",true);rn rn ModelAndView modelAndView=new ModelAndView();rn modelAndView.addObject("hello","1234567");rn modelAndView.setViewName("index");rn return modelAndView;rn rnrn @RequestMapping(value = "/login2",method = RequestMethod.GET)rn public ModelAndView login2()rn rn ModelAndView modelAndView=new ModelAndView();rn modelAndView.setViewName("login");rnrn return modelAndView;rn rnrn @ResponseBodyrn @RequestMapping(value = "/login",method = RequestMethod.POST)rn public ModelAndView Login( HttpServletRequest request, HttpServletResponse response)rn rn HttpSession session=request.getSession();rn session.setMaxInactiveInterval(60);rn ServletContext application=session.getServletContext();rn Set onlineUserSet=new HashSet<>();rn session.setAttribute("username","peter");rn onlineUserSet=(Set)application.getAttribute("onlineUserSet");rn onlineUserSet.add("peter");rn application.setAttribute("onlineUserList",onlineUserSet);rn onlineUserSet=(Set)application.getAttribute("onlineUserSet");rnrn return null;rn rnrn[/code]rnrn无论请求哪个ACTION都没用。rnspringMybatis-servlet.xmlrn[code=java]rnrnrn rn rnrn rn rn rn rn rn rn rn rnrn rn rn[/code]rn搞了一天了,还是404,求教,项目大体结构:[img=https://img-bbs.csdn.net/upload/201702/02/1486035139_284383.jpg][/img]rn 论坛

Spring MVC HTTP ERROR 404

06-05

在学习Spring mvc 的时候遇到这个问题:rnrn请求能够到达Controller,但是Controller在return的时候, 找不到对应的视图文件, 报错rnrnHTTP ERROR 404rnrnProblem accessing /views/index.jsp. Reason:rnrn NOT_FOUNDrnrn但是当我不配置Spring MVC 的时候 用localhost:8888/views/index.jsp 是可以打开这个页面的。rnrn请各位不舍赐教。rnrnweb.xml 代码:rnrn[code=XML]rnrnrn rn rn contextConfigLocationrn classpath*:/spring/*.xmlrn rnrn rn org.springframework.web.context.ContextLoaderListenerrn rn rn rn org.springframework.web.util.Log4jConfigListenerrn rn rn rn springrn org.springframework.web.servlet.DispatcherServletrn 1rn rn 加载/WEB-INF/目录下的所有XML作为Spring MVC的配置文件rn contextConfigLocationrn /WEB-INF/spring/spring-servlet.xmlrn rn rn rn rn springrn /*rn rn rn rn encoding-filterrn org.springframework.web.filter.CharacterEncodingFilterrn rn encodingrn UTF-8rn rn rn forceEncodingrn truern rnrn rnrn rn encoding-filterrn /*rn rn rn rn rn index.htmlrn rnrnrn[/code]rnrndispatcher-servlet.xml 代码:rn[code=XML]rnrnrn rn rn rn rn rn rn rn rn rnrn rn rn rn rn[/code]rnrn[code=Java]rnpackage com.gae.controllers;rnrnimport org.springframework.stereotype.Controller;rnimport org.springframework.web.bind.annotation.RequestMapping;rnrn@Controllerrnpublic class HomeController rn @RequestMapping(value="/home")rn public String Index()rn return "index";rn rnrn[/code]rnrnweb根目录是:warrnrnwarrn|-viewsrn| |-index.jsprn|-WEB-INF 论坛

没有更多推荐了,返回首页