java - spring mvc无法进入controller
PHP中文网
PHP中文网 2017-04-18 09:17:39
0
4
134
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讲师

모든 응답(4)
大家讲道理

이 프로젝트에는 두 개의 컨테이너가 있습니다.
Spring application context 그리고 Spring webapplication context.
은 각각 두 개의 구성 파일 applicationContext.xml{servletName}-servlet.xml에 해당합니다.
그들 간에 관리 개체를 공유하지 않습니다.
구성 파일을 보면 루트 컨테이너 Spring application만 검사되었으며 Spring MVC의 컨테이너(webapplication context)에는 관리 개체가 없음을 알 수 있습니다.
Spring 根容器(application context)에는 매핑을 처리하는 기능이 없으며 요청 매핑을 처리할 수 없습니다.


그래서 구성은 이렇게 되어야 합니다.

으아악
洪涛

spring-mvc.xml을 다음과 같이 수정합니다.

으아악
刘奇

으아악

다음을 시도해 보세요.

으아악
刘奇

Fallen Angel 008의 해법은 옳습니다
· DispatcherServlet웹 컴포넌트의 빈 컴포넌트 로드
· ContextLoaderListener중간 레이어와 데이터 레이어의 빈 컴포넌트 로드
보충
· 제안 spring mvc 프로젝트
에서 컨트롤러와 서비스 컴포넌트 패키지를 분리 · spring-mvc.xml에 정적 리소스 프로세서

를 추가하는 것이 좋습니다.

문제가 해결되지 않은 경우에는 spring mvc helloworld 예제를 읽어보는 것이 좋습니다. 예제 프로젝트도 다운로드할 수 있습니다.

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!