oErnest
oErnest
2017-12-22 13:41
采纳率: 100%
浏览 2.6k
已采纳

SpringMVC地址映射成功但浏览器访问发生404

用idea+tomcat学习SpringMVC,尝试入门工程的时候tomcat启动成功,应用部署成功
,从tomcat日志看,映射也成功了,但是从浏览器访问报404错误,请各位指点.

web.xml没改

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <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>*.form</url-pattern>
    </servlet-mapping>
</web-app>

dispacher-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: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">
    <context:component-scan base-package="controller" />


</beans>

controller是这样的

 package controller;

import com.sun.deploy.net.HttpResponse;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class controller {

    @RequestMapping("/1")
    public ModelAndView controller(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mav = new ModelAndView();
        mav.addObject("msg","a springMVCDemo");
        mav.setViewName("/1.jsp");
        return mav;
    }
}

1.jsp

 <%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2017/12/22
  Time: 20:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>1.jsp</title>
</head>
<body>

${msg}

</body>
</html>

tomcat启动时有如下日志

 十二月 22, 2017 9:18:19 下午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/1] onto handler 'controller'
十二月 22, 2017 9:18:19 下午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/1.*] onto handler 'controller'
十二月 22, 2017 9:18:19 下午 org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
信息: Mapped URL path [/1/] onto handler 'controller'

访问的时候用的url是http://localhost:8080/1这个地址404 但是
http://localhost:8080/1.jsp能正常访问jsp

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • qq_34941558
    SerchOvO 2017-12-22 14:13
    已采纳

    既然你的url-pattern为*.form,那么拦截路径应该为http://localhost:8080/1.form

         <servlet>
            <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>*.form</url-pattern>
        </servlet-mapping>
    

    还未成功,可以加上项目发布名(项目发布名为/可不加)尝试

    点赞 评论
  • l3853999
    l3853999 2017-12-22 13:51

    你的URL呢,给出你的访问地址来看看

    点赞 评论
  • qq_39743338
    qq_39743338 2017-12-22 14:28

    你没配视图解析起吗 ,还有你是ajax访问吗 那你少了 @ResponseBody

    点赞 评论
  • qq_39493105
    冥冥之外 2017-12-22 14:35


    /WEB-INF/web_views/index.jsp


    dispatcher
    .html
    .action
    .json
    .form

    点赞 评论

相关推荐