springmvc(一)---适配器和映射器的使用

一、非注解配置

1、处理器适配器SimpleControllerHandlerAdapter和处理器映射器SimpleUrlHandlerMapping的使用

a、建web工程,将spring的包导入

b、修改web.xml,配置前端控制器



springmvc-handler



springmvc
org.springframework.web.servlet.DispatcherServlet


contextConfigLocation
classpath:springmvc.xml



springmvc
*.action

c、pojo类

package top.einino.pojo;

import java.util.Date;

public class Blog {
private int id;
private String title;
private String content;
private Date createTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}

}

d、写controller类

package top.einino.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;

import top.einino.pojo.Blog;

public class BlogController1 implements Controller{

@Override
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response) throws Exception {

List blogs = new ArrayList();
Blog blog1 = new Blog();
blog1.setId(1);
blog1.setTitle("springmvc");
blog1.setContent("适配器和映射器的使用");
blog1.setCreateTime(new Date());
blogs.add(blog1);
Blog blog2 = new Blog();
blog2.setId(2);
blog2.setTitle("springmvc");
blog2.setContent("适配器和映射器的使用");
blog2.setCreateTime(new Date());
blogs.add(blog2);
Blog blog3 = new Blog();
blog3.setId(3);
blog3.setTitle("springmvc");
blog3.setContent("适配器和映射器的使用");
blog3.setCreateTime(new Date());
blogs.add(blog3);

//返回modelAndView
ModelAndView modelAndView = new ModelAndView();
//将数据放到request域中
modelAndView.addObject("blogs", blogs);
//设置跳转的页面
modelAndView.setViewName("/WEB-INF/blog/blogList.jsp");
return modelAndView;

}

}

e、配置springmvc.xml,配置适配器、映射器和视图解析器

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.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd ">





SimpleControllerHandlerAdapter">

SimpleUrlHandlerMapping">



/queryBlog.action">blogController1





f、写返回的jsp界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt"  prefix="fmt"%>




显示所有博文


所有博文:
















博文id 博文标题 博文内容 博文发布时间 操作
${blog.id } ${blog.title } ${blog.content } 修改



2、使用适配器HttpRequestHandlerAdapter和映射器BeanNameUrlHandlerMapping

a、修改springmvc.xml





HttpRequestHandlerAdapter">

BeanNameUrlHandlerMapping">



b、再写handler

package top.einino.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.ModelAndView;

import top.einino.pojo.Blog;

public class BlogController2 implements HttpRequestHandler{

@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

List blogs = new ArrayList();
Blog blog1 = new Blog();
blog1.setId(1);
blog1.setTitle("springmvc");
blog1.setContent("适配器和映射器的使用");
blog1.setCreateTime(new Date());
blogs.add(blog1);
Blog blog2 = new Blog();
blog2.setId(2);
blog2.setTitle("springmvc");
blog2.setContent("适配器和映射器的使用");
blog2.setCreateTime(new Date());
blogs.add(blog2);
Blog blog3 = new Blog();
blog3.setId(3);
blog3.setTitle("springmvc");
blog3.setContent("适配器和映射器的使用");
blog3.setCreateTime(new Date());
blogs.add(blog3);
//将blogs放到request域中
request.setAttribute("blogs", blogs);
//由request请求转发到/WEB-INF/blog/blogList.jsp下
request.getRequestDispatcher("/WEB-INF/blog/blogList.jsp").forward(request, response);

}

}

二、注解配置

使用处理器适配器RequestMappingHandlerAdapter和处理器映射器RequestMappingHandlerMapping

a、修改springmvc.xml













b、写handler

package top.einino.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import top.einino.pojo.Blog;

@Controller
public class BlogController3 {

@RequestMapping("/queryBlog.action")
public ModelAndView queryBlog(){

List blogs = new ArrayList();
Blog blog1 = new Blog();
blog1.setId(1);
blog1.setTitle("springmvc");
blog1.setContent("适配器和映射器的使用");
blog1.setCreateTime(new Date());
blogs.add(blog1);
Blog blog2 = new Blog();
blog2.setId(2);
blog2.setTitle("springmvc");
blog2.setContent("适配器和映射器的使用");
blog2.setCreateTime(new Date());
blogs.add(blog2);
Blog blog3 = new Blog();
blog3.setId(3);
blog3.setTitle("springmvc");
blog3.setContent("适配器和映射器的使用");
blog3.setCreateTime(new Date());
blogs.add(blog3);
//返回modelAndView
ModelAndView modelAndView = new ModelAndView();
//将数据放到request域中
modelAndView.addObject("blogs", blogs);
//设置跳转的页面
modelAndView.setViewName("blog/blogList");
return modelAndView;

}

}

使用注解的映射器和注解的适配器。(注解的映射器和注解的适配器必须配对使用),在实际开发中可以用来代替两个注解处理器的配置。

三、小结

本博文介绍了三种处理器适配器和三种处理器映射器,但更常用的是注解配置的处理器。

如果有疑问或者对本博文有何看法或建议或有问题的,欢迎评论,恳请指正!

你可能感兴趣的:(springmvc)