pro02总结:spring mvc + jdbc

在pro01的基础上,加入springMVC。

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task" 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/cache   http://www.springframework.org/schema/cache/spring-cache-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/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
           http://www.springframework.org/schema/task    http://www.springframework.org/schema/task/spring-task-3.2.xsd
           http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
       "
    default-lazy-init="true">
    

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
       <property name = "driverClassName" value = "com.mysql.jdbc.Driver"/>
       <property name = "url" value = "jdbc:mysql:///test"/>
       <property name = "username" value = "root"/>
       <property name = "password" value = ""/>
    </bean>
    
    <bean id = "jdbcEmpDAO1" class = "org.rixiang.dao.JdbcEmpDAO1">
          <property name = "dataSource" ref = "myDataSource"></property>
    </bean>
    
    <context:component-scan base-package="org.rixiang"/>
    
    <!-- 支持@RequestMapping请求和Controller映射 -->
    <mvc:annotation-driven/>
    
        <!-- 定义视图解析器viewResolver -->
    <bean id = "viewResolver"
           class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name = "prefix" value = "/WEB-INF/jsp/"/>
           <property name = "suffix" value = ".jsp"/>
    </bean>

</beans>

controller:

package org.rixiang.web;

import java.util.List;

import org.rixiang.dao.EmpDAO;
import org.rixiang.entity.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class EmpListController {
    private EmpDAO dao;
    @Autowired
    public void setDao(EmpDAO dao){
        this.dao = dao;
    }
    @RequestMapping("/emp/list")
    public String execute(Model model){
        List<Emp> list = dao.findAll();
        model.addAttribute("emps",list);
        return "emp_list";
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  

  <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:/applicationContext.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
</web-app>

jsp:

<%@ page language = "java" import = "java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
      <head>
         <title>员工列表示例</title>
      </head>
      
      <body>
          spring-mvc + jdbc操作demo
          <table border="1">
             <tr>
                 <td>编号</td>
                 <td>姓名</td>
                 <td>工资</td>
                 <td>入职时间</td>
             </tr>
             <c:forEach items="${emps}" var="emp">
             <tr>
                  <td>${emp.empno}</td>
                  <td>${emp.ename}</td>
                  <td>${emp.sal}</td>
                  <td>${emp.hiredate}</td> 
             </tr>
            </c:forEach>
          </table>
      </body>
</html>

运行:

原文地址:https://www.cnblogs.com/rixiang/p/5235077.html