1、搭建环境
2、在web.xml中配置DispatcherServlet 前端控制器组件
以上两部是配置spring容器,基于注解的配置和基于xml的配置都一样,具体步骤见上一篇
3、编写Controller组件
package pers.zky.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author Zky
* @version 1.0
* Controller代替了helloController的bean配置
* <bean id="helloController" class="pers.zky.controller.HelloController"></bean>
* 并省了继承org.springframework.web.servlet.mvc.Controller接口
* RequestMapping代替了handlerMapping的配置
* <prop key="/hello/hello.do">helloController</prop>
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/first.do")
public ModelAndView hello1(){
return new ModelAndView("first");
}
@RequestMapping("/second.do")
public ModelAndView hello2(){
return new ModelAndView("second");
}
}
4、applicationContext中开启注解扫描
<context:component-scan base-package="pers.zky"></context:component-scan>
5、applicationContext中开启SpringMVC的扫描驱动
<mvc:annotation-driven></mvc:annotation-driven>
6、 applicationContext中配置视图解析器
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
5、新建jsp页面,完成跳转的功能