2014-11-25 3 views
3

스프링 2.5에서 스프링 4로 webapp을 이전하려고하는데 문제가 발견되었습니다. 동일한 클래스의 두 가지 구성에 대해 작동하는 두 개의 서로 다른 URL이 있습니다.스프링 4 @ 컨트롤러 설정

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
     <property name="mappings"> 
      <props> 
       <prop key="/url1.htm">bean1</prop> 
       <prop key="/url2.htm">bean2</prop> 
       </props> 
      </property> 
    </bean> 

와 콩

<bean id="bean1" class="com.package.Controller" scope="session"> 
    <property name="property" value="value of property"/> 
</bean> 
<bean id="bean2" class="com.package.Controller" scope="session"> 
    <property name="property" value="a different value of the same property"/> 
</bean> 

같은 것을 어떻게 주석이 할 수 있습니다 : 내 이전 버전에서 , 내가 좋아하는 뭔가를?

+0

같이 할 수있는 방법을 복제 할 것과하지 말아야 경우 왜 당신은 포기해야 할 것? Spring 4를 사용한다고해서 SimpleUrlHandlerMapping을 포기해야한다는 의미는 아닙니다. 유즈 케이스가 유효하다면'@ Controller' 대신'Controller'를 사용하십시오. –

+0

하지만 스프링 4에서도 여전히 사용할 수 있습니까 ?? 주석이 달린 @Configuration 클래스에서 어떻게 구성합니까? – Miscillo

+0

인스턴스를 만들고 매핑을 채우기 만하면됩니다. –

답변

1

컨트롤러 클래스에서 @Controller 주석을 사용하고 @RequestMapping 주석을 사용하여 /url1.htm 및 /url2.htm을 매핑하십시오. Look Spring Reference @RequestMapping. 각 클래스에서

@Controller 
@RequestMapping("/url1.htm") 
public class bean1{ 

} 
@Controller 
@RequestMapping("/url2.htm") 
public class bean2{ 

} 

그리고 설정 빈 속성 :

당신이 뭔가를 얻을 것이다. 당신이

@Controller 
public class bean1{ 

    @RequestMapping("/url{id}.htm") 
    public void setBeanProp(@PathVariable int id){ 
    if (id.equals(1)) 
    ... 
    else 
    ... 


    } 
+0

예. 나는 그것을 안다. 나는 각 콩의 모든 메소드를 복제하지 않기를 바란다. 나에게는 시간 낭비이고 미래의 유지 보수 만 복잡하기 때문이다. – Miscillo

+0

"모든 메소드 중복"? 어떤 방법으로 복제하고 있다는 뜻입니까? 두 가지 모두에 넣으려는 컨트롤러 클래스에 메서드가 있으면 클래스에 넣고 두 컨트롤러가 모두 해당 클래스를 확장하도록 만들 수 있습니다. – Jesper