(1) 创建项目01JavaWeb15, 01学号 web project, Java EE 5.0
(2) 创建首页index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <%@ page language= "java" pageEncoding= "gbk" %> < html > < head > < title >第15章 编写Spring第一个程序</ title > </ head > < body > < h1 >第15章 编写Spring第一个程序【首页】杜老师</ h1 > < hr /> < span >1. 开发Spring的HelloWorld程序</ span >< br /> < a href = "HelloClient01.jsp" >Spring之HelloWorld</ a >< br /> < span >实训 Spring在公司管理系统中的应用</ span >< br /> < a href = "CompanyClient01.jsp" >公司发放年终奖</ a >< br /> < span >上机题(2) 两数平均</ span >< br /> < a href = "LSPJClient01.jsp" >查询[23400,4560]两数平均数</ a >< br /> < span >上机题(3-6) 模拟用户管理:用户增删改查</ span >< br /> < a href = "getAllUser01.jsp" >(1) 查询[所有用户信息]</ a >< br /> < a href = "UserClient01.jsp" >(2) 添加、修改、删除用户</ a >< br /> < hr /> < a href = "/html/01JavaWeb.html" >返回章节目录</ a >< br /> </ body > </ html > |
1 |
知识点:文件名 src/.xml
1.为什么使用Spring (1) Spring是一种轻量级框架,轻量级是指该框架技术是非入侵式的,用Spring中开发的系统类不需要依赖于Spring中的类,它不需要容器支持。 (2) 在没使用Spring之前,如果在业务逻辑层中访问数据访问层,需要在业务逻辑层中创建数据访问层的对象,然后使用该对象调用DAO方法。 使用这种方式使业务逻辑层和数据访问层的耦合性非常高,当数据访问层程序发生改动时,则还需要来改动业务访问层的程序,这样就增加了程序员的工作量。 (3) 当Spring出现以后,这种问题就得到了很好的解决。业务逻辑层和数据访问层之间是注入的关系,在业务逻辑层中并不需要创建数据访问层的对象。 2.Spring技术介绍 Spring是一种非常完整的技术,也就是说只使用Spring技术也能开发项目。但是在实际开发中并不这样做,只是让Spring做业务逻辑层,因为Spring的业务处理能力是非常强大的。 在Spring技术中,最经典的就是IoC和AOP技术。 IoC指依赖注入,通过使用该技术可以使业务逻辑层和数据访问层解耦合。 AOP指面向切面编程,通过使用AOP可以很容易的完成切面开发。 |
Spring: 轻量级框架, 松散耦合, src\applicationContext.xml IoC指依赖注入 AOP指面向切面编程 包名 1. cn.xxxxx.service 业务接口 2. cn.xxxxx.service.impl 业务实现类 3. cn.xxxxx.client 客户端 客户端程序:java import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.XXXX.service.接口; public class 测试类{ public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 接口 v =(接口)factory.getBean("bean_id"); //获取业务实现类 v.方法名(参数,...)); //调用业务方法 客户端程序:jsp <%@page import="org.springframework.beans.factory.BeanFactory"%> <%@page import="org.springframework.context.support.ClassPathXmlApplicationContext"%> <%@page import="cn.XXXX.service.接口名"%> <% BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 接口名 v = (接口名)factory.getBean("bean_id"); //获取业务实现类 %> <%=v.方法名(参数,...)%> src\applicationContext.xml <bean id="yyyy" class="带路径的实现的类名"></bean> |
Spring开发环境的搭建:
右击项目 --> MyEclipse --> Add Spring .... --> spring 2.5 core
1 2 3 4 5 | (1) 编写业务接口 (2) 编写业务实现类 (3) 配置业务实现类 applicationContext.xml (4) 编写客户端进行程序(java, jsp) |
在src创建cn.duzhaojiang.service包,创建[HelloService.java]业务接口
1 2 3 4 5 | package cn.duzhaojiang.service; public interface HelloService01 { //定义业务接口 public String SayHello(String name); //定义SayHello抽象方法 } |
创建src创建cn.duzhaojiang.service.impl包,创建[HelloServiceImpl.java]业务实现类
1 2 3 4 5 6 7 8 | package cn.duzhaojiang.service.impl; import cn.duzhaojiang.service.HelloService01; public class HelloServiceImpl01 implements HelloService01 { //定义业务实现类 public String SayHello(String name) { //实现业务方法 return "喂... " +name + ",你妈妈喊你回家吃饭!" ; //返回内容 } } |
在src创建applicationContext.xml业务配置文件(bean:为业务实现类配置别名或id)
1 2 3 4 5 6 7 8 9 | <? xml version = "1.0" encoding = "UTF-8" ?> < beans xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" > < bean id = "hello" class = "cn.duzhaojiang.service.impl.HelloServiceImpl01" ></ bean > </ beans > |
创建src创建cn.duzhaojiang.client包,创建[HelloClient.java]测试类并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package cn.duzhaojiang.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.duzhaojiang.service.HelloService01; public class HelloClient { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); HelloService01 helloService=(HelloService01)factory.getBean( "hello" ); //获取业务实现类 String name= "贾君鹏" ; System.out.println(helloService.SayHello(name)); //调用业务方法 } } |
在WebRoot下创建[Spring之HelloWorld]网页[HelloClient01.jsp]并部署web测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@ page language= "java" pageEncoding= "gbk" %> <%@ page import = "org.springframework.beans.factory.BeanFactory" %> <%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@ page import = "cn.duzhaojiang.service.HelloService01" %> < html > < head > < title >Spring之HelloWorld</ title > </ head > < body > <% BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); HelloService01 helloService=(HelloService01)factory.getBean( "hello" ); //获取业务实现类 %> < h1 >Spring之HelloWorld</ h1 > < hr /> <%= helloService.SayHello( "贾君鹏" ) %> < br /> < hr /> < a href = "/01JavaWeb15" >返回本章目录</ a > </ body > </ html > |
在cn.duzhaojiang.service包,创建员工获取年终奖[Employee.java]业务接口
1 2 3 4 5 | package cn.duzhaojiang.service; public interface Employee { //定义员工 public double getWage(); //获取年终奖方法 } |
创建src创建cn.duzhaojiang.service.impl包,创建普通员工发年终奖[Staff.java]业务实现类
1 2 3 4 5 6 7 8 | package cn.duzhaojiang.service.impl; import cn.duzhaojiang.service.Employee; public class Staff implements Employee { //定义普通职工类 public double getWage() { //实现获取年终奖的方法 return 10000 ; } } |
创建src创建cn.duzhaojiang.service.impl包,创建普通员工发年终奖[Manager.java]业务实现类
1 2 3 4 5 6 7 8 | package cn.duzhaojiang.service.impl; import cn.duzhaojiang.service.Employee; public class Manager implements Employee { //定义普通职工类 public double getWage() { //实现获取年终奖的方法 return 20000 ; } } |
在src创建applicationContext.xml业务配置文件<beans>中添加bean:
1 2 3 | < bean id = "中层领导" class = "cn.duzhaojiang.service.impl.Manager" ></ bean > < bean id = "普通职工" class = "cn.duzhaojiang.service.impl.Staff" ></ bean > |
创建src创建cn.duzhaojiang.client包,创建[CompanyClient01.java]测试类并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package cn.duzhaojiang.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.duzhaojiang.service.Employee; public class CompanyClient { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); Employee employee1=(Employee)factory.getBean( "中层领导" ); //获取经理实现类 System.out.println( "中层领导(经理)的年终奖为:" +employee1.getWage()); //调用业务方法 Employee01 employee2=(Employee01)factory.getBean( "普通职工" ); //获取职工实现类 System.out.println( "普通职工的年终奖为:" +employee2.getWage()); //调用业务方法 } } |
在WebRoot下创建[发放年终奖]网页[CompanyClient01.jsp]并部署web测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language= "java" pageEncoding= "gbk" %> <%@ page import = "org.springframework.beans.factory.BeanFactory" %> <%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@ page import = "cn.duzhaojiang.service.Employee" %> < html > < head > < title >公司发放年终奖</ title > </ head > < body > <% BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); Employee emp1 = (Employee)factory.getBean( "中层领导" ); //获取业务实现类 Employee emp2 = (Employee)factory.getBean( "普通职工" ); //获取业务实现类 %> < h1 >公司发放年终奖</ h1 > < hr /> 中层领导发放年终奖: <%= emp1.getWage() %> < br /> 普通员工发放年终奖: <%= emp2.getWage() %> < br /> < hr /> < a href = "/01JavaWeb15" >返回本章目录</ a > </ body > </ html > |
在cn.duzhaojiang.service包,创建两数平均的[LSPJService01.java]业务接口
1 2 3 4 5 | package cn.duzhaojiang.service; public interface LSPJService01 { public int getPJ( int a, int b); } |
创建src创建cn.duzhaojiang.service.impl包,创建普通员工发年终奖[LSPJServiceImpl01.java]业务实现类
1 2 3 4 5 6 7 8 | package cn.duzhaojiang.service.impl; import cn.duzhaojiang.service.LSPJService01; public class LSPJServiceImpl01 implements LSPJService01 { public int getPJ( int a, int b) { return (a+b)/ 2 ; } } |
在src创建applicationContext.xml业务配置文件<beans>中添加bean:
1 2 | < bean id = "两数平均" class = "cn.duzhaojiang.service.impl.LSPJServiceImpl01" ></ bean > |
创建src创建cn.duzhaojiang.client包,创建[LSPJClient01.java]测试类并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 | package cn.duzhaojiang.client; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.duzhaojiang.service.LSPJService01; public class LSPJClient01 { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); LSPJService01 pj=(LSPJService01)factory.getBean( "两数平均" ); //获取实现类 System.out.println( "2000,4004两数平均为:" +pj.getPJ( 2000 , 4004 )); //调用业务方法 } } |
在WebRoot下创建[两数平均]网页[LSPJClient01.jsp]并部署web测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <%@ page language= "java" pageEncoding= "gbk" %> <%@ page import = "org.springframework.beans.factory.BeanFactory" %> <%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@ page import = "cn.duzhaojiang.service.LSPJService01" %> < html > < head > < title >两数平均</ title > </ head > < body > <% BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); LSPJService01 pj = (LSPJService01)factory.getBean( "两数平均" ); //获取业务实现类 %> < h1 >两数平均</ h1 > < hr /> [23400,4560]两数平均数= <%= pj.getPJ( 23400 , 4560 ) %> < br /> < hr /> < a href = "/01JavaWeb15" >返回本章目录</ a > </ body > </ html > |
创建src创建cn.duzhaojiang.model包,创建普通员工发年终奖[User.java]model类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package cn.duzhaojiang.model; public class User { private String name; private String password; public User(){} public User(String name, String pass){ this .name =name; this .password = pass; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
在cn.duzhaojiang.service包,创建员工获取年终奖[UserClient01.java]业务接口
1 2 3 4 5 6 7 8 9 10 | package cn.duzhaojiang.service; import java.util.List; import cn.duzhaojiang.model.User; public interface UserService01 { public String addUser(String name,String password); public String updateUser(String name,String password); public String deleteUser(String name); public List<User> findAllUser(); } |
创建src创建cn.duzhaojiang.service.impl包,创建普通员工发年终奖[.java]业务实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | package cn.duzhaojiang.service.impl; import java.util.List; import java.util.ArrayList; import cn.duzhaojiang.model.User; import cn.duzhaojiang.service.UserService01; public class UserServiceImpl01 implements UserService01 { public String addUser(String name, String password) { // TODO Auto-generated method stub String b= "添加:" +name+ " 失败. " + "false" ; try { System.out.println( "添加:" +name+ " 成功." ); b= "添加:" +name+ " 成功. " + "true" ; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } public String deleteUser(String name) { String b= "删除:" +name+ " 失败. " + "false" ; try { System.out.println( "删除:" +name+ " 成功." ); b= "删除:" +name+ " 成功. " + "true" ; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return b; } public List<User> findAllUser() { System.out.println( "查找所有人 完成." ); List<User> users = new ArrayList<User>(); users.add( new User( "贝利" , "123456" )); users.add( new User( "梅西" , "654321" )); users.add( new User( "普拉蒂尼" , "666888" )); users.add( new User( "罗纳尔多" , "686868" )); return users; } public String updateUser(String name, String password) { String b= "修改:" +name+ " 失败. " + "false" ; try { System.out.println( "修改:" +name+ " 成功." ); b= "修改:" +name+ " 成功. " + "true" ; } catch (Exception e) { e.printStackTrace(); } return b; } } |
在src创建applicationContext.xml业务配置文件<beans>中添加bean:
1 2 | < bean id = "userManager" class = "cn.duzhaojiang.service.impl.UserServiceImpl01" ></ bean > |
创建src创建cn.duzhaojiang.client包,创建[CompanyClient01.java]测试类并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package cn.duzhaojiang.client; import java.util.List; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.duzhaojiang.model.User; import cn.duzhaojiang.service.UserService01; public class UserClient01 { public static void main(String[] args) { BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); UserService01 userService=(UserService01)factory.getBean( "userManager" ); userService.addUser( "贝克汉姆" , "123" ); userService.updateUser( "梅西" , "123456" ); userService.deleteUser( "贝利" ); List<User> users = userService.findAllUser(); for (User u : users) { System.out.print(u.getName() + "\t" ); //雇员姓名 System.out.print(u.getPassword() + "\t" ); //雇员年龄 System.out.println(); } } } |
在WebRoot下创建[查询所有用户信息]网页[getAllUser01.jsp]并部署web测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | <%@ page language= "java" pageEncoding= "gbk" %> <%@ page import = "java.util.List" %> <%@ page import = "cn.duzhaojiang.model.User" %> <%@ page import = "org.springframework.beans.factory.BeanFactory" %> <%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@ page import = "cn.duzhaojiang.service.UserService01" %> < html > < head > < title >查询[所有用户信息]</ title > </ head > < body > <% BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); UserService01 userService = (UserService01)factory.getBean( "userManager" ); //获取业务实现类 List< User > users = userService.findAllUser(); //将所有记录保存到page范围 pageContext.setAttribute( "uList" ,users); %> < h1 >查询[所有用户信息]</ h1 > < hr /> < table border = "1" > < tr > < td >姓名</ td > < td >口令</ td > </ tr > < c:forEach var = "u" items = "${pageScope.uList}" > <% -- 循环输出信息 -- %> < tr > < td >${u.name }</ td > < td >${u.password }</ td > </ tr > </ c:forEach > </ table > < hr /> < a href = "/01JavaWeb15" >返回本章目录</ a > </ body > </ html > |
在WebRoot下创建[添加、修改、删除用户]网页[UserClient01.jsp]并部署web测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <%@ page language= "java" pageEncoding= "gbk" %> <%@ page import = "org.springframework.beans.factory.BeanFactory" %> <%@ page import = "org.springframework.context.support.ClassPathXmlApplicationContext" %> <%@ page import = "cn.duzhaojiang.service.UserService01;" %> < html > < head > < title >添加、修改、删除用户</ title > </ head > < body > <% BeanFactory factory = new ClassPathXmlApplicationContext( "applicationContext.xml" ); UserService01 userService = (UserService01)factory.getBean( "userManager" ); //获取业务实现类 %> < h1 >添加、修改、删除用户</ h1 > < hr /> 添加用户: <%= userService.addUser( "马拉多纳" , "666666" ) %> < br /> 修改用户: <%= userService.updateUser( "罗马里奥" , "8888888" ) %> < br /> 删除用户: <%= userService.deleteUser( "巴乔" ) %> < br /> < hr /> < a href = "/01JavaWeb15" >返回本章目录</ a > </ body > </ html > |