【笔记】SpringMVC系列框架 [ 2 ]

来源:互联网 发布:ubuntu安装jdk tar.gz 编辑:程序博客网 时间:2024/05/20 08:45

实现Controller 接口,将handleRequest函数改造成分发器

【PersonController】

package com.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.mvc.Controller;import com.dao.PersonDao;import com.entity.Person;public class PersonController implements Controller {    private PersonDao dao;    public void setDao(PersonDao dao) {        this.dao = dao;    }    <!-- 分发器 -->    public ModelAndView handleRequest(HttpServletRequest arg0,            HttpServletResponse arg1) throws Exception {        String url = arg0.getRequestURI();        String m = url.substring(url.lastIndexOf("/") + 1);        if (m.equals("login.do")) {            return this.login(arg0, arg1);        } else if (m.equals("register.do")) {            return this.register(arg0, arg1);        } else {            return null;        }    }    public ModelAndView login(HttpServletRequest arg0, HttpServletResponse arg1)            throws Exception {        String uname = arg0.getParameter("uname");        String pwd = arg0.getParameter("pwd");        ModelAndView m = new ModelAndView();        boolean b = dao.login(uname, pwd);        if (b) {            m.addObject("message", "登录成功");        }        m.setViewName("/ok.jsp");        return m;    }    public ModelAndView register(HttpServletRequest arg0, HttpServletResponse arg1)            throws Exception {        String id = arg0.getParameter("id");        String pname = arg0.getParameter("pname");        String pwd = arg0.getParameter("pwd");        ModelAndView m = new ModelAndView();        Person p = new Person();        p.setId(Integer.valueOf(id));        p.setPname(pname);        p.setPwd(pwd);        dao.add(p);        m.addObject("message","注册ok");        m.setViewName("/ok.jsp");        return m;    }}

【applicationContext.xm】

<?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:mvc="http://www.springframework.org/schema/mvc"      xsi:schemaLocation="            http://www.springframework.org/schema/beans       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-3.0.xsd      http://www.springframework.org/schema/mvc         http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            ">    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/athl_ajax" />        <property name="driverClass" value="com.mysql.jdbc.Driver" />        <property name="user" value="root" />        <property name="password" value="root" />    </bean>    <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">        <constructor-arg ref="dataSource" />    </bean>    <bean id="dao" class="com.athl.dao.PersonDao">        <property name="qr" ref="queryRunner" />    </bean>    <bean id="controller" class="com.athl.controller.PersonController">        <property name="dao" ref="dao" />    </bean></beans>
0 0
原创粉丝点击