java - spring mvc无法进入controller
PHP中文网
PHP中文网 2017-04-18 09:17:39
0
4
132
web.xml

    <!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app 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_3_0.xsd"
         version="3.0">
    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/config/spring/spring-ctx.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/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>

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

spring-ctx.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"
       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">

    <context:component-scan base-package="com.prs.dps"/>

    </beans>

spring-mvc

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

    <mvc:annotation-driven enable-matrix-variables="true"/>

    <bean      
        class="org.springframework.web.servlet.view.
        InternalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp" />
    </beans>

controller

    package com.prs.dps;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    @Controller
    public class Test {
        @RequestMapping(value = "/toindex",method = RequestMethod.GET)
        public String toIndex(){
            return "index";
        }
    }
PHP中文网
PHP中文网

认证高级PHP讲师

membalas semua(4)
大家讲道理

Terdapat dua bekas dalam projek ini.
Spring application context dan Spring webapplication context.
sepadan dengan dua fail konfigurasi masing-masing applicationContext.xml dan {servletName}-servlet.xml.
Mereka tidak akan berkongsi objek terurus antara mereka.
Anda boleh melihat daripada fail konfigurasi anda bahawa hanya bekas akar Spring application telah diimbas dan tiada objek terurus Spring MVC dalam bekas webapplication context () .
dan Spring 根容器(application context) tidak mempunyai fungsi untuk mengendalikan pemetaan dan tidak boleh mengendalikan pemetaan permintaan .


Jadi konfigurasi sepatutnya seperti ini.

// spring-ctx.xml
....
// 根容器不扫描@Controller注解的类。
<context:component-scan base-package="gq.zpf_fly.first">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
...

// spring-mvc.xml
....
// 不是用默认过滤规则(指定包内全部扫描), 手动设置规则,只扫描@Controller注解的类。
<context:component-scan base-package="gq.zpf_fly.first" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<!--启用 MVC注解(@Controller,@RequestMapping)实现URL映射-->
<mvc:annotation-driven/>
洪涛

Ubah suai spring-mvc.xml seperti berikut:

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

    <mvc:annotation-driven/>
    
    <context:component-scan base-package="com.prs.dps"/>
    <bean      
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"         p:prefix="/views/" p:suffix=".jsp" />
            <!-- 视图解释类 -->
    
    </beans>
刘奇
<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/*</url-pattern>
</servlet-mapping>

Cuba ini sebaliknya:

<servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
</servlet-mapping>
刘奇

Penyelesaian Fallen Angel 008 betul
· DispatcherServletMuat komponen kacang komponen Web
· ContextLoaderListenerMuatkan komponen kacang lapisan tengah dan lapisan data
Tambahan
· Cadangan Asingkan pakej pengawal dan komponen perkhidmatan dalam projek mvc spring
· Adalah disyorkan agar subjek menambah pemproses sumber statik spring-mvc.xml dalam

Jika masalah belum selesai, disyorkan untuk membaca contoh spring mvc helloworld Contohnya agak ringkas dan jelas Anda juga boleh memuat turun projek contoh

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!