我是Spring MVC框架的新手。我正在尝试编写一个简单的控制器(从AbstractController扩展)并希望返回一个简单的视图(home.jsp),但似乎从不调用控制器的handleRequestInternal()。我在下面给出了我的代码。任何帮助都将受到高度赞赏。
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="hiperlink is trimmed "
xmlns:xsi="hiperlink is trimmed "
xsi:schemaLocation="hiperlink is trimmed " >
<servlet>
<servlet-name>students</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>students</servlet-name>
<url-pattern>/students/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/spring</taglib-uri>
<taglib-location>/WEB-INF/spring-form.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>hiperlink is trimmed java.sun.com/jsp/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>hiperlink is trimmed java.sun.com/jsp/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
学生-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="hiperlink is trimmed "
xmlns:xsi="hiperlink is trimmed " xmlns:context="hiperlink is trimmed" xmlns:jaxws="hiperlink is trimmed "
xsi:schemaLocation="hiperlink is trimmed ">
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="myService" class="net.ochinto.student.service.MiscellaneousService"/>
<bean name="/home.htm" class="net.ochinto.student.web.HomePageController">
<property name="myService" ref="myService"/>
</bean>
</beans>
HomePageController.java
public class HomePageController extends AbstractController {
private static final Logger log = LoggerFactory.getLogger(HomePageController.class);
public HomePageController() {
log.debug("HomePageController constructor()");
}
protected ModelAndView handleRequestInternal(
HttpServletRequest request, HttpServletResponse response)
throws Exception {
log.debug("HomePageController handleRequestInternal()");
List<Student> students = myService.getStudentList();
return new ModelAndView("home", "students", students);
}
private MiscellaneousService myService;
public void setMyService(MiscellaneousService myService) {
log.debug("HomePageController setMyService() - start");
this.myService = myService;
log.debug("HomePageController setMyService() - end");
}
}
欢迎页面没问题,但是当我写/students/home.htm 404返回时!请帮忙。
答案 0 :(得分:0)
你说:
欢迎页面没问题,但是当我写/students/home.htm 404返回时!请帮忙。
您的配置只有一个URL映射,一个用于/home.htm。如果您希望/students/home.htm指向某个位置,则需要添加其他映射。
查看Spring文档的Handler Mapping部分以获取更多信息。