新手springMVC遇到问题,求大神指点intellij-idea平台

rt,刚学springmvc想试一下,然后。。。

我的配置文件:

mvc-dispatcher-servlet.xml

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

    <context:annotation-config></context:annotation-config>

    <!--指明 controller 所在包,并扫描其中的注解-->
    <context:component-scan base-package="com.lzq.zsk">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>


    <!-- 静态资源(js、image等)的访问 -->
    <mvc:default-servlet-handler/>
    <!-- 开启注解 -->
    <mvc:annotation-driven></mvc:annotation-driven>
    <!--ViewResolver 视图解析器-->
    <!--用于支持Servlet、JSP视图解析-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

 MainController.java
 
package com.lzq.zsk.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/hello")
public class MainController {

    @RequestMapping("/mvc")
    public String helloMVC(){
        System.out.println("helo");

        return "home";
    }
}

clipboard.png

clipboard.png

clipboard.png

阅读 7.9k
7 个回答

引用你的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>

<display-name>Archetype Created Web Application</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

按此配置,spring没有读mvc-dispatcher-servlet.xml这个配置文件

在web.xml最上面添加下面的配置,保证项目启动一开始就读配置

<!--spring加载配置-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

另外建议问题有补充说明最好在问题里加,不要在评论里加

试试spring boot吧

应该是配置的问题,你的web.xml文件呢?

web.xml 有配置 dispatcher-servlet.xml?

没有包含类文件,项目是maven来建的吗,如果是需要配置pom.xml中的plugin

clipboard.png

或者是在下图这个地方导入需要的依赖包(不建议)

clipboard.png

一个是依赖包的版本问题(高版本名字改了),一个是core的配置不全。

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

web.xml少了上面这句,把Spring容器集成到 Web 应用里面

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题