2013-04-30 52 views
0

我有这个在web.xml:Spring如何从jsp中知道使用什么控制器?

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

<!-- Dispatching handled by StaticFilter --> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

我有这个dispatcher-servlet.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-2.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:annotation-config/> 

    <!-- Activates scanning of @Repository --> 
    <context:component-scan base-package="com.pronto.mexp" /> 

    <!-- View Resolver for JSPs --> 
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="requestContextAttribute" value="rc"/> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 
     <property name="prefix" value="/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
</beans> 

我有这样的AlertsController:

@Controller 
public class AlertsController { 

    protected final Log logger = LogFactory.getLog(getClass()); 

    @Autowired 
    private AlertManager alertManager; 

    @RequestMapping("/alerts") 
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 

     // display in view 
     logger.info("Returning alerts view"); 

     List<Alert> alerts = alertManager.getAlerts(); 
     request.setAttribute("alerts", alerts); 

     return new ModelAndView(); 
    } 

    public void setAlertManager(AlertManager alertManager) { 
     this.alertManager = alertManager; 
    } 
} 

而且我有这在alerts.jsp中:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> 

<h3>ALERTS</h3> 

<table border="1"> 
<c:forEach var="alert" items="${alerts}"> 
    <tr> 
    <td>${alert.hostname}</td> 
    <td>${alert.message}</td> 
    <td>${alert.program}</td> 
    <td><fmt:formatDate value="${alert.date}" dateStyle="medium"/></td> 
    </tr> 
</c:forEach> 
</table> 

但是,当我启动应用程序并将浏览器指向localhost:8080/alerts.jsp时,我只能看到标题“ALERTS”,而没有其他东西。这就像Spring不知道使用AlertsController。我知道我要离开一些关键配置,但我看不到它。

回答

2

你正在做的与你应该的相反。这不是JSP知道要使用哪个控制器,而是Controller知道要呈现哪个视图(JSP)。该控制器由其url映射执行,该映射在@RequestMapping属性中定义。当你直接访问你的JSP时,你根本就没有经历过Spring。因此请尝试使用网址http://localhost:8080/context/alerts代替context与Web应用程序的上下文路径。

+0

谢谢!还有一个问题 - 我在哪里可以找到Web应用程序的上下文路径? – barclay 2013-04-30 20:33:45

+0

如果你可以用'localhost:8080/alert.jsp'访问你的JSP,那么它是空白的。但通常在部署应用程序时进行配置。 – NilsH 2013-04-30 20:46:31

0

我的同事指出我的调度员servlet.xml中还缺少MVC指令在我的控制器扫描注释:

<!-- Configures the @Controller programming model --> 
<mvc:annotation-driven/> 

因此,即使当我localhost:8080/alerts指着我的浏览器(因为我没有背景路径配置),它仍然失败。一旦我添加了mvc指令,就会调用控制器,并将动态内容发送给jsp。

+0

但是,由于我的同事拒绝注册SO帐户,因此我将给予@NilsH他的一半答案。 IDK为什么! – barclay 2013-04-30 20:52:13

0

一行答:你在呼唤它,你必须调用错误的方法/警报不alerts.jsp

但为什么你得到这个空白页,因为你调用JSP不直接由控制器设定值,你把JSP文件根目录下

<property name="prefix" value="/"/> 

,使其接近,最好是把它放在WEB-INF,以防止直接访问

<property name="prefix" value="/WEB-INF/jsps/"/> 
+0

我得到空白页面,因为控制器没有被调用。看到我的答案,上面。但是就你而言,还有你之前的NilsH,我注意到,我错误地称它为/后退思考。 – barclay 2013-05-01 15:48:02

+0

“控制器没有被调用”因​​为你正在调用“/alerts.jsp”,而控制器正在监听“/ alerts”@RequestMapping(“/ alerts”) – 2013-05-01 15:57:53

+0

对不起,但正如我在上面的答案中提到的,即使在我打电话给“/警报”它仍然失败。控制器没有被调用,因为Spring不知道为控制器寻找注释。 – barclay 2013-05-01 16:21:56

相关问题