1.配置前端控制器
在web.xml中配置DispatcherServlet(前端控制器)本质上是一个servlet
所以配置与servlet有相似之处
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置默认加载路径 为classpath:springmvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern><!-- 匹配以.action为结尾的路径-->
</servlet-mapping>
2.在springmvc.xml中配置
- 配置handler※
1.name=”/queryItems.action” 访问时在地址栏写的地址为”/queryItems.action”
以.action结尾,上文前端控制器中servlet mapping中匹配相同
2.class=”test.ssm.controller.ItemsController” 写的controller全路径 –>
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置handler-->
<bean name="/queryItems.action" class="test.ssm.controller.ItemsController"></bean>
<!-- 处理器映射器 将bean的name作为url进行查找 需要在配置handler时指定beanname(url) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping.class"></bean>
<!-- 处理器适配器
所有的适配器处理器都实现 HandlerAdapter -->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 处理器解析器
配置解析jsp的解析器
写的是jsp页面 所以配置的是jsp页面显示 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/></beans>
controller的Java文件代码如下
返回的是一个ModelAndView值
package test.ssm.controller;
public class ItemsController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<String> li=new ArrayList<String>();
li.add("百度");
li.add("搜狐");
li.add("新浪");
ModelAndView MAV=new ModelAndView();
MAV.addObject("info",li);//类似于servlet中的setAttrivute方法
MAV.setViewName("/show.jsp");//作用到哪个jsp页面
return MAV;
}
}
注解方式实现 简洁明了
web.xml文件如下
<servlet>
<servlet-name>springmvc2</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置默认加载文件
contextConfigLocation 配置springmvc加载的配置文件 -->
<!--不配置 则默认加载的是/WEB-INF/servlet名称-servlet.xml(springmvc-servlet-class) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc2.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc2</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
springmvc2.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!--自动配置映射器和注解器 实际中使用 方便操作-->
<mvc:annotation-driven></mvc:annotation-driven>
<!--指定扫描的包-->
<context:component-scan base-package="test.ssm.controller2"></context:component-scan>
<!--自动配置jsp视图控制器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>