初步学习SpringMVC

来源:互联网 发布:电商数据分析软件 编辑:程序博客网 时间:2024/06/11 06:55

第一步,单纯使用SpringMVC的时候,必须用到的jar包

第二步,了解SpringMVC的核心模块

DispatcherServlet:是单例的,跟servlet是一样的。主要也是对请求进行过滤和转发的。

<!-- 配置前端控制器DispatcherServlet -->  <servlet>  <servlet-name>springmvc</servlet-name>  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <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>  </servlet-mapping>


第三步,配置处理适配器HandlerAdaper

在springmvc.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-4.2.xsd        http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd">    <!-- 处理器适配器:HandlerAdapter。所有处理器适配器都实现了HandlerAdapter接口-->    <!-- SimpleControllerHandlerAdapter适配器能执行实现了Controller接口的Handler     所以,现在配置了这个适配器的话,所有的处理器Handler必须要实现Controller接口才行。-->    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> </beans>

第四步,开发Handler处理器

ModelAndView里面以键值对的形式存储数据,然后在jsp页面,取出

package ssm.controller;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;public class ItemsController1 implements Controller {public ModelAndView handleRequest(HttpServletRequest arg0,HttpServletResponse arg1) throws Exception {// TODO Auto-generated method stubList<Items> itemsList=new ArrayList<Items>();Items items1=new Items();items1.setName("联想笔记本");items1.setPrice(6000f);items1.setDetail("E430");Items items2=new Items();items2.setName("苹果手机");items2.setPrice(5000f);items2.setDetail("phone6手机");itemsList.add(items1);itemsList.add(items2);ModelAndView modelAndView =new ModelAndView();//相当于request的setAttribute,在jsp页面通过itemsList取数据modelAndView.addObject("itemsList", itemsList);//指定视图modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");return modelAndView;}}

第五步,配置处理器Handler

在springmvc.xml里面对Handler进行配置,为相应的请求添加处理类

<!-- 配置Handler -->    <bean name="/queryItems.action" class="ssm.controller.ItemsController1" />

第六步,配置处理器映射器HandlerMapping

<!-- 配置处理器映射器    将bean的name作为url进行查找,需要在配置Handler时指定beanname(就是url) --><bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />

第七步,配置视图解析器ViewResolver

<!-- 配置视图解析器     进行jsp解析,默认使用jstl标签,classpath下得有jstl的包--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

转载:http://blog.csdn.net/eson_15/article/details/51689648

写得特别好!






0 0
原创粉丝点击