나는 web.xml 파일이 있습니다스프링은 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>
내가 디스패처-servlet.xml 파일이 있습니다
@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;
}
}
그리고 내가 가진 :
<?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이 있습니다 alerts.jsp의 this :
<%@ 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를 사용하는 것을 모르는 것과 같습니다. 내가 중요한 설정을 빠뜨리는 것을 알고 있지만 나는 그것을 볼 수 없다.
감사를 방지하기 위해 WEB-INF에 넣어 더 나은 액세스 할 수 있는지
그래서 루트에서 JSP 파일을 가하고 있습니다! 또 하나의 질문 - 웹 앱의 컨텍스트 경로는 어디에서 찾을 수 있습니까? – barclay
'localhost : 8080/alert.jsp'로 JSP에 접근 할 수 있다면 비어 있습니다. 하지만 대개 응용 프로그램을 배포 할 때이를 구성합니다. – NilsH