$(function() {
//可以的获得失去焦点
$("#loginName").blur(function() {
var usernameInput = $(this).val();
$.ajax( {
url : "<%=basePath%>login/StaffAction!findAll",
async : true,
type : "post",
datatype : "json",
//说的是成功的返回了请求,就是不知道怎样安置请求回来的数据 还是说aja需要返回值
success : function(staff) {
alert(typeof (staff));
var obj = JSON.parse(staff)
//var obj = eval('(' + staff + ')');
alert(typeof obj)
alert(obj.loginName)
},
error : function() {
alert("请求失败")
},
});
})
})
alert(typeof (staff));
var obj = JSON.parse(staff)
alert(obj.loginName);
$("#t").append(obj.loginName);//这个是正确的
var str = "";
str += "<tr>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"<td>" + obj.loginName + "</td>" +
"</tr>";
$("#tbody-result").append(str); //可以的。利用这个可以写的。
------------------------------------------------------------------------------
1、ajax的运用:
导入json.js 的jar包。
然后将json的字符串转化成json的对象 :
这一步注意的点:两种方法:
var
obj
=
JSON.parse(staff)
var obj = eval('(' + staff + ')');
2、在action里面返回得到的json格式 :
有三种方法: 1 jsonlib
//JSONObject jsonObject = JSONObject.fromObject(staff);
// JSONArray jsonArray = JSONArray.fromObject(staffs);//
// 这个是返回的数据的格式用的是jsonlib
// result = jsonArray.toString();
2、就是利用fastjson (阿里巴巴的软件)
List<Staff> staffs = staffService.findAll();
staff = staffs.get(0);
result = JSON.toJSON(staff).toString();
这个是最方便的,无论是对象还是数组都可以用这个方法来转 。这几个方法不懂的最好的方式就是利用百度,多查查百度,你就一定可以明白的。
3、Gson:goole 这个没怎么用过多查查边度就好了。
4、关于前端页面的数据的获得:
console.log(data)//控制台输出data 在页面上控制台输出语句
data=JSON.parse(data)
----------------------------------------------------------------------------------------------------------
二 :最重要的事情就是:
1、整合SSH框架的时候 struts2 里面写的是返回json的格式 ,配置好ssH extends
2、返回的时候要有servcletContext.getResponse来返回 ,string的返回值要是NONE(也就是没有返回值,否则的话)
1、你返回的值不是json格式,里面会有斜号。只能用你自己的方式去写框架返回的格式是不对的。
ServletActionContext.getResponse().setContentType(
"application/json;charset=UTF-8");
ServletActionContext.getResponse().getWriter().print(result);
return NONE;
--------------------------------------------------------------------------
用其他的方式不行 ,不是因为后面写了两个配置文件的原因: 不是extends-json的原因
只能按照这种办法来做。
----------------------------------------------------------------------------------
SSH框架整合:顺序直接拷贝项目就是最好的整合发布到你的github上就可以
SSH整合 顺序: 这个部分你最好还是要学会配置hieberbate的关系 最好是能够使用一键反键生成可以生成多种关系最好是配置好表的各种关系
1---先打好框架各个包建起来,然后在把每一个类的实体类 dao层 service层 web层 全部都给搭建好。把道层什么的全部都写完
2----开始各种配置:
需要注意的点:1建实体类的时候,实体类一定要配置的跟你hibernate一样,hibernated的proprity中的元素的元素,实体类当中一定要有,不然的话就会一直报错的。(还有就是出现报错的时候,你一定不要着急,你可以继续看把所有的错误都看完以后再去找错误,也不一定非要从第一个错误开始找,也可以从最常见的错误开始找);
3----配置 1、applicationContext
1-先配置上面的源文件(其实所有的源文件除了一点,其他的可以全部引用进去直接粘贴复制)
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/springcontext.xs> --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2、开始配置数据库或者数据源 (这里用的是另一种配置文件,具体不知道但是通常用的是这个)
<!--数据源连接配置c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.p6spy.engine.spy.P6SpyDriver"></property>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/ee19_crmdb?characterEncoding=utf-8"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
3、然后配置sessionFactory
<!--由Spring 代理sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
<!--自己先写一遍,搞回来然后自己再看一遍视频,然后整理一遍背记下来 回去多看视频特别是难点 你有那么多项目 你可以多看看里面的难点 -->
</property>
<property name="mappingLocations" value="classpath*:com/gh/crm/**/*.hbm.xml"></property>
</bean>
在这里其实都已经把hibernate全部写入到spring里面去了
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
4、配置事务管理器:
<!--事务管理器 -->
<bean id="txManger"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5、配置事务通知:
<!--事务通知 切面 -->
<tx:advice id="txAdvice" transaction-manager="txManger">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
6、配置aop 切面的事务管理
<!-- aop事务管理 -->
<aop:config >
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.gh.crm.stuff.service..*.*(..))"/>
</aop:config>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
7、就是引入部分的 bean-staff.xml(全路径
)
<!--开始引入spring的xml文件 -->
<import resource="com/gh/crm/stuff/bean-staff.xml"/>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
局部的——bean-staff.xml(除了配置applicationContext.xml的源文件以外,就只需要配置service层和dao层)
<!--这就是做好了一个员工的类了 -->
<bean id="StaffDao" class="com.gh.crm.stuff.dao.StaffDaoImp">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="staffService" class="com.gh.crm.stuff.service.StaffServiceImp">
<property name="StaffDao" ref="StaffDao"></property>
</bean>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
二:开始配置struts2的文件:
1、配置开发者模式
2、启动动态调用
3、引入其他部分struts2的文件
<struts>
<!--开发者模式 -->
<constant name="struts.devMode" value="true"></constant>
<!--启动动态调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<!--加载各部分的struts 文件 -->
<include file="com/gh/crm/stuff/struts-staff.xml"></include>
</struts>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2、配置局部的struts2的文件
需要注意的点:1、写命名空间 2 、要用json的格式的时候就必须写:
extends
=
"json-default"
3、就是哪一个返回的值 ,就要写返回的类型,值要写进parm里面 ,不然的话就是所有的数据全部都会转成json的格式
<struts>
<package name="login" namespace="/login" extends="json-default">
<!--由spring 实例化action,我们只需要引用 -->
<action name="StaffAction" class="com.gh.crm.stuff.web.StaffAction">
<result name="success" type="redirect">../html/frame.html</result>
<result name="staff_findAll" type="json">
<param name="root">result</param>
</result>
</action>
</package>
</struts>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
三、配置hibernate的文件:
hibernate的文件这个要写在model里面 。其他的就是要写它的实体类的属性名
多运用hibernate的查询的方法
<hibernate-mapping package="com.gh.crm.stuff.model">
<class name="Staff" table="crm_staff">
<id name="staffId">
<generator class="uuid"></generator>
<!--
这个是由hibernate自动生成 我现在的目的最重要的目的就是为了把框架好好的练熟练 把ssh全部写好
然后再把SSM几个特别是mybaits和springmvc好好的学习好,然后把spring好好的多看几遍
-->
</id>
<!--类的属性和数据库对象关系,默认不写column 就以属性做且数据库的字段列 -->
<property name="loginName"></property>
<property name="loginPwd"></property>
<property name="staffName"></property>
<property name="gender"></property>
<property name="onDutyDate"></property>
<property name="postId"></property>
</class>
</hibernate-mapping>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
四、就是给SSH做一个juint的测试:
测试的话:第一就是要更改加载juint4的环境变量。
第二1、就是要在类名上加载juninSpring的注解juint4的测试 2、就是加载总的spring的配置文件applicationContext
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestStaff {
@Autowired
private StaffService staffService;
//测试验证密码
@Test
public void test(){
Staff s = new Staff();
s.setLoginName("123");
s.setLoginPwd("123");
Staff staff = staffService.login(s);//已经在道层返回出来了
System.out.println(staff);
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
多利用线上的w3cShcool 这个应该是更方便的,看的更清楚的