上一篇大概讲述了SpringMVC的初步配置,这里我们重点讲述注解部分的内容。
1.配置web.xml
与上一篇一样,请参考上篇配置。
2.编写处理请求的控制器
@Controller
public class TestSpringMVC {
@RequestMapping("/hello.action")
public ModelAndView test(){
System.out.println("Helloworld");
ModelAndView model=new ModelAndView();
model.setViewName("success");
return model;
}
在类的上面加上注解@Controller就可以标识该类是一个Controller类,可以接收用户请求并做处理等。
在方法前加上@RequestMapping(“/hello.action”)表示 这个方法可以处理用户请求,并规定了用户请求的url。
3.编写视图对象
具体请参考上一篇
4.配置springmvc.xml
<mvc:annotation-driven></mvc:annotation-driven>
<context:component-scan base-package="com.web"></context:component-scan>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="" />
<property name="suffix" value=".jsp" />
</bean>
<mvc:annotation-driven></mvc:annotation-driven> 这个相当于配置了HandlerAdapter 和HandlerMapping。
<context:component-scan base-package="com.web"></context:component-scan>扫描了这个com.web包下所有的类,在容器中进行bean配置,并将其注入到容器中
5.测试运行
结果同上篇一样