Spring框架

使用Spring框架的优势:

  1. 轻量级的IOC容器/IOC也叫控制反转,后来改名为DI也叫依赖注入
  2. 一致的事务管理要么都成功,要么都失败。
  3. 面向切面编程(AOP)

实现依赖注入有以下方式:

  1. set方法注入
  2. 构造器注入
  3. 注解注入

依赖注入的概念:

当篇写一个复杂的java应用程序时,应用程序时,应用程序类应该尽可能的独立。于其他java类来增加这些类可重用可能性,当进行单元测试时,可以使他独立于其他类进行测试。依赖注入有时会被称为配线有助于见这些类粘合在一起,并且在同一时间让它们保持独立。依赖注入实现的3种方式:

  1. 构造函数注入
  2. setting方法注入
  3. 注解注入

面向编程(AOP):

一个程序中跨越多个点功能称为横切关注点。这些横切关注点在概念上独立于程序的业务逻辑,有各种各样常见的很好的关于方面的例子,比如日志记录,声明性事物安全性,和缓存等等,在oop中模块化的关键单元是类,而在aop中模块化的关键单元是方面,aop帮助你将横切关注点从它们所影响的对象中分离出来,然而依赖注入帮助你将你的应用程序对象从彼此中分离出来。Spring框架的AOP模块提供了。面向方面的 程序设计实现,允许你定义拦截器方法和切入点,可以实现将应该被分开的代码干净的分开功能。

数据访问:

  1. JDBC提供了数据库连接操作
  2. ORM提供了对象关系映射处理
  3. 事务:提供了编程式和声明式事务的管理。

框架构造:

在这里插入图片描述

构思图:

链接
index.jsp
Class.java
Web.xml
Spring-servlet.xml
OK.jsp
  • 核心容器
  • context
  • beans
  • croe

练习

1.index.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
     <h1>用户注册页面</h1>
       <form action="ok/la">
           用户名:<input name="text" type="text"/>
           密码:<input name="pass" type="password"/>
           身份证号:<input name="text1" type="text"/>
           <input type="submit" value="提 交"/>
       </form>
       <a href="ok/selet/23">点击查询</a>
</body>
</html>

2.class.java

package com.anzhuo.cm.cotroller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.anzhuo.cm.pojo.PoJo;


@Controller
@RequestMapping("ok")
public class houtaiDemo {
	
    @RequestMapping("la")
	public String form(PoJo pojo){
    	String wenben=pojo.getText();
    	String mima=pojo.getPass();
    	String sfzh=pojo.getText1();
    	
    	 if(!wenben.equals("")&&!mima.equals("")&&!sfzh.equals("")){
    		 System.out.println("传过来的值为:"+wenben+"和"+mima+"和"+sfzh);
    		 return "xianying";
    	 }else{
    		return "xianying1";
    	 }
    	 
	}
    
    @RequestMapping("selet/{name}")
   	public String form1(@PathVariable("name") String name){
   	    if(name.equals("23")){
   	    	System.out.println(name);
   	    	return "xianying";
   	    }else{
   	    	return "xianying1";
   	    }
   		
   	}
    
    @RequestMapping(method=RequestMethod.GET)
   	public String form2(){
   		  System.out.println("我进来了");
   		return "xianying";
   	}
   
   
}

3.classPoJo.java

package com.anzhuo.cm.pojo;

public class PoJo {
   public String text; 
   public String pass;
   public String text1;
public String getText() {
	return text;
}
public void setText(String text) {
	this.text = text;
}
public String getPass() {
	return pass;
}
public void setPass(String pass) {
	this.pass = pass;
}
public String getText1() {
	return text1;
}
public void setText1(String text1) {
	this.text1 = text1;
}
}

4.spring-servlet.xml

<?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:aop="http://www.springframework.org/schema/aop"
	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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <context:component-scan base-package="com.anzhuo.cm.cotroller"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/jsp/"></property>
       <property name="suffix" value=".jsp"></property> 
    </bean>

</beans>

5.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>From-Demo1</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
	<servlet>
		<servlet-name>spring</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	
		<load-on-startup>1</load-on-startup>
	</servlet>

		<servlet-mapping>
		<servlet-name>spring</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

6.OK.jsp/No.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>成功</h1>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>失败</h1>
</body>
</html>

所需要的包

commons-logging-1.2.jar
spring-aop-4.3.5.RELEASE.jar
spring-beans-4.3.5.RELEASE.jar
spring-context-4.3.5.RELEASE.jar
spring-core-4.3.5.RELEASE.jar
spring-expression-4.3.5.RELEASE.jar
spring-web-4.3.5.RELEASE.jar
spring-webmvc-4.3.5.RELEASE.jar

总结构思

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值