2014-10-01 233 views
2

我是网页开发人员以及Spring MVC的新手。我正在测试控制器的一个愚蠢的例子,但是,似乎控制器从来没有被调用过,或者至少它没有按预期工作。Spring MVC:如何获取控制器的正确URL?

目标是在“test.jsp”页面中单击一个超链接,并直接调用“customers.jsp”页面,同时调用控制器,以便将消息传递给“customers.jsp”页。

现在我的“customers.jsp”页面正确显示,但没有显示传输的消息。网址是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> 

更新那些谁这篇文章后可能会看到:

确实存在通过@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" /> 

希望它能帮助:)

回答

1

Javatan是正确的: 尝试

<protocol>/<host>:<port>/WebProject/customers 
您定义的前缀和后缀

里面的InternalResourceViewResolver bean定义,这些值将被用于查找尤尔jsp文件应用。与您的应用程序创建的网址没有任何共同之处。

你的url路径是在控制器内部定义的(不在InternalResourceViewResolver中)。 因此,在应用程序名称之后,您正在使用在控制器内定义的路径。

您也可以在方法:)定义@RequestMapping(“/用户”)

检查,如果你在你的bean定义行有,看起来像这样:

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

该定义将扫描com.project.web.controllers包(和子包)内的所有类以查找标记有@Controller注释的类。您可以通过查询更多在这里: 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更改为“customers”时,我在url'WebProject/customers'处得到了404错误。任何建议? – Truely 2014-10-02 07:47:31

+0

首先我将web.xml改为/改为/*。这样所有的路径都将被映射,而不仅仅是/。 – Beri 2014-10-02 07:52:51

+0

其次你有像你这样的代码定义:。这里让我们假设你的控制器位于包my.app.controllers中。该行将扫描该软件包中的所有类以搜索控制器。 – Beri 2014-10-02 07:54:42

0

你直接调用customer.jsp 。相反,您必须调用控制器,然后将该请求传递给customer.jsp。在HREF属性中,您需要提及您的控制器中@RequestMapping注释中给出的值。

对于更详细的例子,请访问我的博客Here

+0

你的博客是非常有帮助的。但是当我将href更改为“customer”(这是@RequestMapping中给出的值)时,我在页面'/ WebProject/customers'上收到错误404。看起来控制器从未被调用过。你有什么主意吗? – Truely 2014-10-02 12:52:27

1

您可以使用该代码来获得服务器的网址:

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

而你映射到控制器/客户

这样的网址应该是:

http://localhost:8080/WebProject/customers/customers.jsp 
相关问题