我是Spring的新手并尝试使用控制器配置一个简单的调度程序servlet,但我似乎无法让它工作。我想要http://localhost:8080/1/dispatcher/index打印出来#34; welcome"在控制台中。我的老板说,由于映射,它也应该自动将你重定向到index.jsp页面。
WelcomeController.java
@Controller
public class WelcomeController {
@RequestMapping("/index")
public ModelAndView welcome()
{
System.out.println("welcome entered");
}
}
的web.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>
答案 0 :(得分:0)
首先,似乎所指示的代码无法编译。 welcome()
方法应返回ModelView类型的对象,指示要显示的视图。
在这个方法中你可以将视图放到解析器中,如果你想要显示index.jsp,你可以将index放入viewName方法。