2014-12-20 4 views
0

저는 스프링 4로 작업하기 시작했습니다. 자바 구성으로 스프링 4 MVC 프로젝트를 설정하고 싶습니다. IntelliJ Idea 13.1.6을 사용합니다. 먼저 Spring MVC 3.2 템플릿으로부터 새로운 Spring 4 MVC 프로젝트를 생성한다. 그런 다음 Java Spring 구성 파일을 추가하지만 응용 프로그램을 실행하면 홈 컨트롤러 대신 hello 템플릿 컨트롤러에서 "Hello world"라는 프로그램이 표시됩니다. 여기 내 프로젝트 파일입니다스프링 4 HTTP 상태 404

enter image description here

그리고 :

HelloController :

package com.spitter.mvc; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping("/") 
public class HelloController { 
    @RequestMapping(method = RequestMethod.GET) 
    public String printWelcome(ModelMap model) { 
     model.addAttribute("message", "Hello world!"); 
     return "hello"; 
    } 
} 

RootConfig :

package spittr.config; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.ComponentScan.Filter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.FilterType; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
@Configuration 
@ComponentScan(basePackages={"spitter"}, 
     excludeFilters={ 
       @Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class) 
     }) 
public class RootConfig { 
} 

SpittrWebAppInitializer :

,691,363 여기 내 프로젝트 구조210
package spittr.config; 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 
public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 
    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 
    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class<?>[] { RootConfig.class }; 
    } 
    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class<?>[] { WebConfig.class }; 
    } 
} 

HomeController :

package spittr.web; 
import static org.springframework.web.bind.annotation.RequestMethod.*; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
public class HomeController { 
    @RequestMapping(value="/", method=GET) 
    public String home() { 
     return "home"; 
    } 
} 

MVC - 디스패처-servlet.xml 파일 :

<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.spitter.mvc"/> 

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/pages/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

</beans> 

웹 app.xml :

<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 

    <display-name>Spring MVC 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> 
</web-app> 

Spitter.iml :

<?xml version="1.0" encoding="UTF-8"?> 
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> 
    <component name="FacetManager"> 
    <facet type="web" name="Web"> 
     <configuration> 
     <descriptors> 
      <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" /> 
     </descriptors> 
     <webroots> 
      <root url="file://$MODULE_DIR$/src/main/webapp" relative="/" /> 
     </webroots> 
     <sourceRoots> 
      <root url="file://$MODULE_DIR$/src/main/resources" /> 
      <root url="file://$MODULE_DIR$/src/main/java" /> 
     </sourceRoots> 
     </configuration> 
    </facet> 
    <facet type="Spring" name="Spring"> 
     <configuration /> 
    </facet> 
    </component> 
    <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false"> 
    <output url="file://$MODULE_DIR$/target/classes" /> 
    <output-test url="file://$MODULE_DIR$/target/test-classes" /> 
    <content url="file://$MODULE_DIR$"> 
     <sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" /> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/resources" isTestSource="false" /> 
     <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> 
     <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> 
     <excludeFolder url="file://$MODULE_DIR$/target" /> 
    </content> 
    <orderEntry type="inheritedJdk" /> 
    <orderEntry type="sourceFolder" forTests="false" /> 
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test-mvc:1.0.0.M2" level="project" /> 
    <orderEntry type="library" scope="PROVIDED" name="Tomcat 8.0.151" level="application_server_libraries" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-core:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-web:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-context:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-aop:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-beans:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-expression:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet.jsp:jsp-api:2.1" level="project" /> 
    <orderEntry type="library" name="Maven: org.springframework:spring-webmvc:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:3.2.0.RELEASE" level="project" /> 
    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.2" level="project" /> 
    <orderEntry type="library" name="Maven: javax.servlet:jstl:1.2" level="project" /> 
    </component> 
</module> 

왜이 코드는 홈 컨트롤러를 실행하지 만 안녕하세요 컨트롤러는 필자가 어떻게 고칠 수 있습니까?

나는 Spitter.iml 파일에 문제가 있다고 생각하는데, 스프링은 Java 클래스 구성 대신 web.xml 구성 만 볼 수 있습니다.

+2

문제를 재현하는 코드를 최소한으로 줄여야합니다. 모든 코드를 통과하는 것은 자체 프로젝트입니다! –

+0

컨트롤러가 동일한 요청 매핑 경로를 사용하고 있습니다. – JamesB

+0

아니, 그렇지 않습니다. hello 컨트롤러에서 @RequestMapping ("/ hello")으로 요청 매핑을 변경하면 HTTP Status 404가 발생합니다. ( – Sayaki

답변

1

내가 보는 주된 문제는 구성과 몇 가지 작은 세부 사항입니다. 예에서 프로젝트를 구성하는 두 가지 방법 (Java 및 XML)을 사용하고 있습니다. Java 기반 구성을 사용하기를 선호하지만 충돌을 일으키지 않고 결합 할 수는 있습니다.

예를 들어 기본 패키지가있는 mvc-dispatcher-servlet.xml에 HelloController가 표시되어 있기 때문에 HelloController를 실행합니다.

<context:component-scan base-package="com.spitter.mvc"/> 

그리고 SpittrWebAppInitializer가 web.xml로 바뀝니다. 올바른 패키지를 지정하려면 XML 파일을 삭제하고 RootConfig를 수정해야합니다.

@ComponentScan(basePackages={"spittr"} 

이렇게 WebConfig도 비슷합니다.

@Configuration 
@EnableWebMvc 
@ComponentScan("spittr.web") 
public class WebConfig extends WebMvcConfigurerAdapter { 

    // Configure a JSP view resolver 
    @Bean 
    public ViewResolver viewResolver() { 
     InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 
     resolver.setViewClass(JstlView.class); 
     resolver.setPrefix("/WEB-INF/pages/"); 
     resolver.setSuffix(".jsp"); 
     return resolver; 
    } 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     // TODO Auto-generated method stub 
     super.addResourceHandlers(registry); 
    } 
} 

그리고 당신은 당신이 오류가있을 것이다 @ComponentScan하는 패키지 com.spitter.mvc을 WebConfig에 추가하는 경우에만 예를 들어, 여러분의 컨트롤러에 고유 한 경로를 가질 수 있습니다.