我是 spring 的新手,并尝试使用控制器配置一个简单的调度程序 servlet,但我似乎无法让它工作。 我想 http://localhost:8080/1/dispatcher/index 在控制台中打印出"欢迎"。 我的老板说它还应该自动将您重定向到索引.jsp页面,因为映射。
欢迎控制器.java
@Controller
public class WelcomeController {
@RequestMapping("/index")
public ModelAndView welcome()
{
System.out.println("welcome entered");
}
}
网络.xml
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/dispatcher/</url-pattern>
</servlet-mapping>
</web-app>
springDispatcherServlet-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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:component-scan base-package="com.paymon" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
首先,指示的代码似乎无法编译。welcome()
方法应返回一个类型为模型视图的对象,该对象指示要显示的视图。
在此方法中,您可以将视图放入解析器,如果要显示索引.jsp,则将索引放入viewName方法。