Spring与Mybatis整合(二)

一.Web.xml配置文件

  <!-- 监听spring启动 -->
  <listener>
   <description>Spring监听器</description>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 设置Spring加载时的配置文件位置,默认位置在web-inf/lib下 -->
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml</param-value>
   </context-param>
  

二.Servelet基类

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

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

/**
 * Servlet基类
 * 自定义控制器基类
 */
public class BaseController extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public BaseController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		//处理中文
		request.setCharacterEncoding("utf-8");
		
		 // 获得提交的路径 :/find.do  
		String path=request.getServletPath();
		System.out.println(path);
		
		//获得方法名
		String methodName=path.substring(1,path.indexOf("."));
		
		//利用反射
		try {
			Method method=getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
			 // 执行相应的方法  
			//在当前对象中调用,传递参数request与response,获得返回结果
			String url=method.invoke(this, request,response).toString();
			
			System.out.println(url);
			
			//判断如果返回的url是以redirect开始,则是重定向
			if(url.startsWith("redirect:")){
				//BookController.do?act=Deletes
				response.sendRedirect(url.substring(9,url.length()));
			}else{
				 // 转发
				request.getRequestDispatcher(url).forward(request, response);
			}
		  
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		out.flush();
		out.close();
	}


}

具体实现类:

 

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.hlx.biz.BookBiz;
import com.hlx.entity.Book;


@WebServlet(name = "BookController", urlPatterns = "*.action")
// 继续父类
public class BookController extends BaseController {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//处理业务逻辑接口
	private BookBiz biz;

	/**
	 * 初始化 创建实例
	 */
	@Override
	public void init() throws ServletException {

		// 在Servlet初始化时获取Spring上下文对象(ApplicationContext)
		ApplicationContext ac = WebApplicationContextUtils
				.getWebApplicationContext(this.getServletContext());
		  //从ApplicationContext中获取userService
		biz = (BookBiz)ac.getBean("bookBizImpl");
	}

	/**
	 * 图书列表Action
	 * 
	 * @return
	 */
	public String all(HttpServletRequest request, HttpServletResponse response) {
		//System.out.println("biz=" + biz);
		request.setAttribute("lists", biz.getAllBooks());
		return "list.jsp";
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值