2014-10-01 4 views
2

저는 Spring MVC뿐만 아니라 웹 개발의 초보자입니다. 컨트롤러에 대한 어리석은 예제를 테스트 중이지만 컨트롤러가 호출 된 적이 없거나 적어도 예상대로 작동하지 않는 것 같습니다.스프링 MVC : 컨트롤러에 적합한 URL을 얻는 방법?

목표는 "test.jsp"페이지에서 하이퍼 링크를 클릭하고 컨트롤러가 호출 된 상태에서 "customers.jsp"페이지로 이동하여 메시지가 "customers.jsp"페이지로 전달되도록하는 것입니다. 페이지.

이제 "customers.jsp"페이지가 올바르게 표시되지만 전송 된 메시지는 표시되지 않습니다. URL은 http://localhost:8080/WebProject/view/customers.jsp입니다.

문제를 찾는데 도와 주시겠습니까? 컨트롤러에 매핑 된 URL 때문이라고 생각합니다. 하지만 컨트롤러에 매핑 할 올바른 URL을 확인할 수있는 방법이 있습니까? 감사! 마지막에

* 업데이트 *

프로젝트 구조는 다음과 같다 : 여기

--webapp |--view |--*.jsp |--WEB-INF |--*.xml

내 코드입니다 :

test.jsp를

<a href='<c:url value="customers.jsp"/>'>show customers</a> 

또는

<a href="customers.jsp">show customers</a> 

모두 제대로 작동합니다.

customers.jsp

<h2>return: ${list}</h2> 

컨트롤러 :

@Controller 
@RequestMapping("/customers") 
public class MenuController { 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView listCustomers() { 
     ModelAndView model = new ModelAndView("customers"); 
     model.addObject("list", "controller: a list of customers"); 
     return model; 
    } 
} 

및 구성 :

web.xml의

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

<servlet-mapping> 
    <servlet-name>myDispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

myDispatcher-servlet.xml에

이 게시물 나중에 볼 수 있습니다 사람들을 위해
<context:component-scan base-package="com.example.spring.controller" /> 
<context:component-scan base-package="com.example.spring.model" /> 

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

UPDATE :

@javafan 및 @Beri으로 표시된 바와 같이 URL 맵핑의 문제가 실제로 있습니다. 하지만 URL을 <a href="customers">show customers</a>으로 변경 한 후에도 오류 404가 발생했습니다 (http://localhost:8080/WebProject/customers). 실제로 이것은 패키지 스캔 오류로 인해 발생하므로 컨트롤러를 찾을 수 없습니다. 이 문제를 해결하려면 단순히 "myDispatcher-servlet.xml에"파일에서 줄을 변경 :

<context:component-scan base-package="com.example.spring.controller" /> 
<context:component-scan base-package="com.example.spring.model" /> 

<context:component-scan base-package="controller" /> 
<context:component-scan base-package="model" /> 

이 도움이 :) 당신은 직접 customer.jsp를 호출

답변

1

Javatan를 잘 내 블로그를 방문 : 시도

<protocol>/<host>:<port>/WebProject/customers 
당신이 접두사와 접미사를 정의

내부의 InternalResourceViewResolver bean 정의는, 그 값은 yur에서 JSP 파일을 찾는 데 사용되는 신청. 그리고 응용 프로그램이 만든 URL과 공통점이 없습니다.

url 경로는 InternalResourceViewResolver가 아닌 컨트롤러 내부에서 정의됩니다. 그래서 응용 프로그램 이름 뒤에 컨트롤러 내부에 정의 된 경로를 사용하고 있습니다.

또한 방법 :)에 @RequestMapping ("/ 고객")를 정의 할 수 있습니다

다음과 같습니다

확인 당신이 당신의 빈 정의 라인에있는 경우 :

<context:component-scan base-package="com.project.web.controllers"/> 

이 정의는 것입니다 @Controller 어노테이션으로 표시된 클래스를 찾으려면 com.project.web.controllers 패키지 (및 서브 패키지) 내의 모든 클래스를 검사하십시오. 당신은 자세한 내용은 여기를 확인할 수 있습니다 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html, 장 :

  • 17.3.1

    - 난이 도움이되기를 바랍니다

처럼

  • 17.2 - 어떻게 web.xml을 보이는 컨트롤러를 등록하는 방법에 대해 설명합니다. 좋아, JSP와 지역의 예제를 실행 한 : web.xml을

    <?xml version="1.0" encoding="UTF-8"?> 
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
        id="WebApp_ID" version="3.0"> 
    
        <servlet> 
         <servlet-name>myDispatcher</servlet-name> 
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
         <load-on-startup>1</load-on-startup> 
        </servlet> 
    
        <servlet-mapping> 
         <servlet-name>myDispatcher</servlet-name> 
         <url-pattern>/</url-pattern> 
        </servlet-mapping> 
    
        <context-param> 
         <param-name>contextConfigLocation</param-name> 
         <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> <!-- Here you have your spring bean definition --> 
        </context-param> 
    
        <listener> 
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
        </listener> 
    </web-app> 
    

    컨트롤러 :

    @Controller 
    public class MenuController { 
    
        @RequestMapping(value="/customers", method = RequestMethod.GET) 
        public ModelAndView listCustomers() { 
         ModelAndView model = new ModelAndView("customers"); 
         model.addObject("list", "controller: a list of customers"); 
         return model; 
        } 
    } 
    

    디스패처-servlet.xml에 당신이 얻을이 코드를 사용할 수 있습니다

    <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    
        <context:component-scan base-package="com.programcreek.helloworld.controller" /> 
    
        <bean 
         class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
         <property name="prefix"> 
          <value>/views/</value> 
         </property> 
         <property name="suffix"> 
          <value>.jsp</value> 
         </property> 
        </bean> 
    </beans> 
    
  • +0

    답변 주셔서 감사합니다! 설명이 참으로 유용합니다. 하지만 href를 "고객"으로 변경하면 url 'WebProject/customers'에서 404 오류가 발생합니다. 어떠한 제안? – Truely

    +0

    첫 번째 web.xml 변경 / ~ /*. 모든 경로가 매핑 될 수 있도록 /뿐 아니라 – Beri

    +0

    두 번째로 다음과 같이 코드 정의에 포함되어 있습니다 : . 여기에서는 컨트롤러가 my.app.controllers 패키지에 있다고 가정합니다. 이 줄은이 패키지의 모든 클래스를 검색하여 컨트롤러를 검색합니다. – Beri

    0

    희망 . 대신 컨트롤러를 호출하여 customer.jsp에 요청을 전달해야합니다.HREF 속성에서 컨트롤러의 @RequestMapping 주석에 지정된 값을 언급해야합니다.

    자세한 예를 들어, Here

    +0

    블로그가 많은 도움이 될 것입니다. 하지만 내가 href를 @RequestMapping에 주어진 값인 "customer"로 변경하면'/ WebProject/customers' 페이지에서 404 오류가 발생합니다. 컨트롤러가 호출되지 않는 것 같습니다. 너는 어떤 생각을 가지고 있니? – Truely

    1

    서버의 URL :

    "<a href="${pageContext.request.contextPath}/customers.jsp">show customers</a>" 
    

    그리고 당신은 고객

    /에 컨트롤러를 매핑하는 그래서 URL은 다음과 같아야합니다

    http://localhost:8080/WebProject/customers/customers.jsp 
    
    관련 문제