配置文件:
Strtus:1、客户端浏览器发出HTTP请求。2、根据web.xml配置,该请求被FilterDispatcher接收。3、根据struts.xml配置,找到需要调用的Action类和方法,并通过IoC方式,将值注入给Aciton。4、Action调用业务逻辑组件处理业务逻辑,这一步包含表单验证。5、Action执行完毕,根据struts.xml中的配置找到对应的返回结果result,并跳转到相应页面。6、返回HTTP响应到客户端浏览器。 struts2入口是一个filter过虑器,即前端过滤器,例如:/*
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="www" extends="struts-default" namespace="/ddd"> <interceptors> 拦截器(底层18个默认拦截器) <interceptor name="inter" class="com.down.WoInterceptor"></interceptor> <interceptor-stack name="intercp"> <interceptor-ref name="defaultStack"></interceptor-ref> <interceptor-ref name="inter"></interceptor-ref> </interceptor-stack> </interceptors> <default-interceptor-ref name="intercp"></default-interceptor-ref> <action name="down" class="com.down.DownloadAction" method="down"> <result name="success" type="stream"> <param name="contentType">application/octet-stream</param> <param name="inputName">fileStream</param> <param name="contentDisposition">attachment;filename=${filename}</param> <param name="bufferSize">1024</param> </result> </action> <action name="list" class="com.down.DownListAction" method="getlist"> <!-- <result name="success">/index.jsp</result> --> <result name="list">/show.jsp</result> </action> </package> </struts>
|
属性文件:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 常量配置 --> <!-- 把stuts标签中的form表单主题设置为简易模式:否则struts会动态植入很多标记 --> <constant name="struts.ui.theme" value="simple"></constant> <!-- 开启动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 设置编码格式 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 最大上传量:按字节B单位 --> <constant name="struts.multipart.maxSize" value="31457280"></constant> <!-- struts默认扩展 --> <constant name="struts.action.extension" value="action,do,,"></constant> <!-- 是否为开发模式 --> <constant name="struts.devMode" value="true"></constant> <!-- 更改配置文件后重新加载 --> <constant name="struts.configuration.xml.reload" value="true"/> </struts> |
Spring:可以解决对象创建以及对象之间依赖关系的一种框架。
且可以和其他框架一起使用;Spring与Struts, Spring与hibernate
(起到整合(粘合)作用的一个框架)
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- IOC容器配置:spring 对象的创建都交给IOC容器 --> <!-- 1.对象的模式 scope:模式 创建对象 个数 默认 "singleton" 项目启动时 唯一的一个 prototype 使用时 每次创建一个 2.懒初始化 lazy-init="true" :懒初始化 :只对单例模式有效:第一次使用时创建对象,唯一的一个对象 3.初始化和销毁方法 init-method="user_init" 对象创建之后执行 destroy-method="user_destory" 上下文对象销毁之前执行 init() destory <bean id="sstr" class="java.lang.String"> <constructor-arg value="李艾"></constructor-arg> </bean> <bean id="user11" class="com.hello.User" scope="singleton" lazy-init="false" init-method="user_init" destroy-method="user_destory"></bean> --> <bean id="user22" class="com.hello.User" scope="prototype" > <constructor-arg value="101" index="0" name="uid" type="int"/> <constructor-arg value="jack中文" index="1" name="uname"/> </bean> <!-- <bean id="user33" class="com.hello.User" scope="prototype" > <constructor-arg value="101" index="0" name="uid" type="int"/> <constructor-arg index="1" name="uname" ref="sstr"/> </bean> --> </beans> |
Hibernate:orm思想的一种体现,能把对象的数据直接保存到数据库中,能从数据库中直接拿到一个对象
主配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <!-- hibernate主配置 --> <hibernate-configuration> <!-- session 工厂 --> <session-factory> <!-- 连接数据库的配置 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <!-- 方言的定义 :转换sql语句的标准 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 显示转换后的sql语句 --> <property name="hibernate.show_sql">true</property> <!-- 加载映射关系:实体:属性-表:字段 --> <mapping resource="com/entity/Emp.hbm.xml" /> </session-factory> </hibernate-configuration> |
映射:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.entity"> <!-- 一个class是一个表与实体的映射关系 --> <class name="Emp" table="emp"> <!-- 主键映射 :name 属性 column字段 --> <id name="eid" column="id"></id> <!-- 普通字段的映射关系 type:数据类型 java类型 :全类名 java.lang.String java.util.Date hibernate 数据类型 :全部小写:最终会转换成java的类型 --> <property name="ename" column="ename" type="java.lang.String"></property> <property name="sal" type="double"></property> <property name="hiredate" column="hiredate" type="date"></property> </class> </hibernate-mapping> |
springMVC:action层springmvc的入口是一个servlet,即前端控制器例如:*.action
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <bean name="/hello" class="com.action.Hello2"/> <!-- 映射器:把name映射为URL 可省略 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 适配器 :请求发送过来时,在所有(直接或间接 )实现Controller接口的Action中找到匹配的Action 可省略 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 视图解析器: 解析ModelAndView :获取视图资源 【定义真实路径时 可省略】--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/> <!-- 定义逻辑视图时必须指定前缀和后缀 真实路径=前缀+逻辑视图+后缀 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"></property> </bean> --> </beans> |
springmvc与struts2的区别
springmvc的入口是一个servlet,即前端控制器,例如:*.action struts2入口是一个filter过虑器,即前端过滤器,例如:/* 2)springmvc是基于方法开发,传递参数是通过方法形参,可以设计为单例 struts2是基于类开发,传递参数是通过类的属性,只能设计为多例 3)springmvc通过参数解析器是将request对象内容进行解析成方法形参,将响应数据和页面封装成 ModelAndView对象,最后又将模型数据通过request对象传输到页面 struts采用值栈存储请求和响应的数据,通过OGNL存取数据
|
Mybatis:orm的一种体现
主配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
<!--设置日志 --> <settings> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <!-- 设置别名 --> <typeAliases> <typeAlias type="com.hello.EmpDao" alias="empDao"/> </typeAliases> <!-- 配置环境 --> <environments default="env"> <environment id="env" > <!-- 事物的管理交给jdbc --> <transactionManager type="JDBC" /> <!-- 用连接池管理数据库连接 --> <dataSource type="pooled"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8" /> <property name="username" value="root" /> <property name="password" value="123456" /> </dataSource> </environment> </environments>
<!-- 加载映射文件 --> <mappers> <mapper resource="com/hello/EmpMapper.xml" /> <!-- 映射文件名必须和类名一致且在同一包下 <mapper resource="com/hello/Emp" /> <package name="com.hello"/> --> </mappers> </configuration> |
映射文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="hello">
<!-- resultMap :表示实体和表的映射:如果实体属性和表字段完全对应,该配置可省略 type:类型:完整的类名 :包名.类名 id:该映射关系的唯一标识 id :主键映射 property :属性 column:字段:和属性相同时,可省略 result:非主键映射
--> <resultMap type="com.hello.Emp" id="emp"> <id property="eid" column="eid" javaType="int"/> <result property="ename" column="ename"/> <result property="sal" column="sal"/> <result property="hiredate" column="hiredate"/> </resultMap>
<!-- insert update delete 标记通用:本质执行的是定义的sql语句 executeUpdate() select 标记不能通用 --> <!-- 以? 的形式加载 insert into emp values(?,?,?,?), #{}的值以get的方法设置进去 , 也可以${}的形式加载,不过,${}的形式没有?,直接将值加载在sql语句中--> <!-- 静态sql语句 --> <insert id="add" parameterType="empDao"> insert into emp values(#{eid},#{ename},#{sal},#{hiredate}) </insert> <delete id="delete" parameterType="int"> delete from emp where eid=#{eid} </delete> <update id="update" parameterType="map"> update emp set ename=#{ename},sal=#{sal},hiredate=#{hiredate} where eid=#{eid} </update> <!-- 如果参数是简单类:int double String 名称任意写,建议和形参一致
resultType :把结果集中的每一行封装为一个对象 按照字段为对象设置值:如果字段和属性不一致,赋值失败
resultMap:属性和字段不一致时; 按照映射的配置 把结果集中的字段对应成属性
--> <!-- 动态sql语句 --> <select id="findall" resultMap="emp" parameterType="map"> select eid,ename,sal,hiredate from emp <where> <if test="eid!=null"> and eid = #{eid} </if> <if test="ename!=null"> and ename like #{ename} </if> <if test="sal!=null"> and sal >= #{sal} </if> <if test="hiredate!=null"> and hiredate <= #{hiredate} </if> </where> <if test="size!=null"> limit <if test="start!=null"> #{start}, </if> #{size} </if> </select> <update id="update1" parameterType="map"> update emp <set> <if test="ename!=null"> ename = #{ename} , </if> <if test="sal!=null"> sal = #{sal} , </if> <if test="hiredate!=null"> hiredate = #{hiredate} , </if> </set> where eid=#{eid} </update>
<delete id="delete2" parameterType="int"> delete from emp where eid in <foreach collection="array" item="ids" open="(" close=")" separator=","> #{ids} </foreach> </delete> <delete id="delete3" parameterType="int"> delete from emp where eid in <foreach collection="list" item="ids" open="(" close=")" separator=","> #{ids} </foreach> </delete> <sql id="ziduan"> <trim suffixOverrides=","> <if test="ename!=null"> ename , </if> <if test="sal!=null"> sal , </if> <if test="hiredate!=null"> hiredate , </if> </trim> </sql> <sql id="zhi"> <trim suffixOverrides=","> <if test="ename!=null"> #{ename} , </if> <if test="sal!=null"> #{sal} , </if> <if test="hiredate!=null"> #{hiredate} , </if> </trim> </sql> <insert id="insert" parameterType="map"> insert into emp (<include refid="ziduan"/>) values (<include refid="zhi"/>) </insert> </mapper> |