springmvc都是通过其中央控制器DispatcherServlet来进行转发的,所以在web.xml文件里面需要增加相应的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< servlet >
< servlet-name >springServlet</ servlet-name >
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class >
< init-param >
< param-name >contextConfigLocation</ param-name >
< param-value >/WEB-INF/spring-mvc.xml</ param-value >
</ init-param >
< load-on-startup >1</ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >springServlet</ servlet-name >
< url-pattern >/</ url-pattern >
</ servlet-mapping >
|
如果没有配置contextConfigLocation,Spring mvc默认配置文件为“/WEB-INF/[servlet名字]-servlet.xml“即“/WEB-INF/springServlet-servlet.xml“
一、简单的controller配置
springServlet-servlet.xml配置文件如下
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<< span ></ span >!-- HandlerMapping -->
< bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />
<!-- HandlerAdapter -->
< bean class = "org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />
<!-- ViewResolver -->
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" />
< property name = "prefix" value = "/WEB-INF/view/" />
< property name = "suffix" value = ".jsp" />
</ bean >
< bean name = "/hello" class = "dk.web.controller.HelloWorldController" ></ bean ></ beans >
|
HelloWorldController
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public class HelloWorldController implements Controller {
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//1、收集参数、验证参数
//2、绑定参数到命令对象
//3、将命令对象传入业务对象进行业务处理
//4、选择下一个页面
ModelAndView mv = new ModelAndView();
//添加模型数据 可以是任意的POJO对象
mv.addObject( "message" , "Hello World,spring mvc" );
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName( "hello" );
return mv;
}
}
|
/WEB-INF/view/hello.jsp
1
2
3
4
5
6
7
8
9
10
11
12
|
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<! DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
< html >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< title >Hello World</ title >
</ head >
< body >
${message}
</ body >
</ html >
|
在web容器里面部署及运行你的工程,在浏览器里面访问 ip:port/hello 即可在页面看到 Hello World,spring mvc
二、基于注解anotation的controller配置
Spring2.5 引入注解式处理器支持,通过@Controller 和@RequestMapping注解定义我们的处理器类。并且提供了一组强大的注解:需要通过处理器映射DefaultAnnotationHandlerMapping和处理器适配器AnnotationMethodHandlerAdapter来开启支持@Controller 和 @RequestMapping 注解的处理器。
@Controller:用于标识是处理器类;
@RequestMapping:请求到处理器功能方法的映射规则。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< beans >
<!-- HandlerMapping -->
< bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<!-- HandlerAdapter -->
< bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- ViewResolver -->
< bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
< property name = "viewClass" value = "org.springframework.web.servlet.view.JstlView" />
< property name = "prefix" value = "/WEB-INF/view/" />
< property name = "suffix" value = ".jsp" />
</ bean >
</ beans >
|
PS:spring3.2版本后 DefaultAnnotationHandlerMapping由RequestMappingHandlerMapping取代;AnnotationMethodHandlerAdapter由RequestMappingHandlerAdapter取代
HelloWorldController
1
2
3
4
5
6
7
8
9
10
|
@Controller
public class HelloWorldController {
@RequestMapping (value= "/helloAnotation" ,method=RequestMethod.GET)
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp){
ModelAndView mv = new ModelAndView();
mv.addObject( "message" , "Hello World,spring anotation" );
mv.setViewName( "hello" );
return mv;
}
}
|
在web容器里面部署及运行你的工程,在浏览器里面访问 ip:port/helloAnotation即可在页面看到 Hello World,spring anotation
时间: 2024-10-27 07:31:48