$(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 這個應該是更方便的,看的更清楚的