java web後臺開發SSM框架(Spring+SpringMVC+MyBaitis)搭建與優化

 

1、ssm框架搭建

1.1建立項目

新建項目後規劃好各層的包。javascript

1.2導入包

 

搭建SSM框架所需包百度雲連接: http://pan.baidu.com/s/1cvKjL0css

1.3整合spring與mybatis

調整spring與mybatis配置文件html

1.4建立、編寫配置文件:

 

 

myBatis-config.xml文件java

<?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>

<!-- 經過別名簡化對類的使用

<typeAliases>

      <typeAlias type="cn.itcast.entity.Dept" alias="Dept" />

</typeAliases>

   <mappers>

      <mapper resource="cn/itcast/entity/DeptMapper.xml" />

   </mappers>

      -->

</configuration>

 

applicationContext.xmlmysql

<?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:tx="http://www.springframework.org/schema/tx"

   xmlns:aop="http://www.springframework.org/schema/aop"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:p="http://www.springframework.org/schema/p"

   xsi:schemaLocation="http://www.springframework.org/schema/beans

   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-3.2.xsd

   http://www.springframework.org/schema/tx

   http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

 

  

   <!-- 配置數據源,記得去掉myBatis-config.xml的數據源相關配置 -->

   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        

      <property name="driverClass" value="com.mysql.jdbc.Driver" />

      <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=UTF-8" />

      <property name="user" value="root" />

      <property name="password" value="root" />     

   </bean>

   <!-- 配置session工廠 -->

   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dataSource" />

      <property name="configLocation" value="classpath:myBatis-config.xml" />

   </bean>

  

   <!-- 配置事務管理器,管理數據源事務處理-->

   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

      <property name="dataSource" ref="dataSource" />

   </bean>

   <!-- 配置事務通知 -->

   <tx:advice id="advice" transaction-manager="transactionManager">

      <tx:attributes>

<!-- 默認只處理運行時異常,可加rollback-for="Exception/Throwable"等處理全部異常或包括錯誤 -->

         <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>

         <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>

         <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>

         <tx:method name="*" propagation="SUPPORTS"/>

      </tx:attributes>

   </tx:advice>

   <!-- 配置切面織入的範圍,後邊要把事務邊界定在service層 -->

   <aop:config>

      <aop:advisor advice-ref="advice" pointcut="execution(* cn.itcast.scm.dao.impl.*.*(..))"/>

   </aop:config>

   <!-- 配置SessionTemplate,已封裝了繁瑣的數據操做-->

   <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">

      <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>    

   </bean>

  

   <context:component-scan base-package="*"/>

     

</beans>

 

1.5編寫實體及sql映射文件

  如沒有建庫表,先建庫表,可參考以下sql:jquery

drop database if exists mybatis;

create database mybatis CHARACTER SET UTF8;

use mybatis;

 

create table dept(

    dept_id int primary key auto_increment,

    dept_name varchar(50),

    dept_address varchar(50)

);


insert into dept(dept_name,dept_address) values('研發部一部','廣州');

insert into dept(dept_name,dept_address) values('研發部二部','廣州');

insert into dept(dept_name,dept_address) values('研發部三部','深圳');

select * from dept;

 

 編寫實體類git

public class Dept implements Serializable {

   private Integer deptId;

   private String deptName;

   private String deptAddress;

   public Integer getDeptId() {

      return deptId;

   }

   public void setDeptId(Integer deptId) {

      this.deptId = deptId;

   }

   public String getDeptName() {

      return deptName;

   }

   public void setDeptName(String deptName) {

      this.deptName = deptName;

   }

   public String getDeptAddress() {

      return deptAddress;

   }

   public void setDeptAddress(String deptAddress) {

      this.deptAddress = deptAddress;

   }

   @Override

   public String toString() {

      return "Dept [deptId=" + deptId + ", deptName=" + deptName

            + ", deptAddress=" + deptAddress + "]";

   }

 
}

 

  sql映射文件,並將相關信息映射到mybatis-config.xml文件。web

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="cn.itcast.scm.entity.DeptMapper">

   <resultMap type="Dept" id="deptResultMap">

      <id property="deptId" column="dept_id" />

      <result property="deptName" column="dept_name" />

      <result property="deptAddress" column="dept_address" />

   </resultMap>

   <!-- id和命名空間用來定位SQL語句,parameterType表示參數的類型,resultMap返回類型 -->

   <select id="selectDept" parameterType="Integer" resultMap="deptResultMap">

      <!--參數的寫法#{deptID} -->

      select * from dept where dept_id=#{deptID}

   </select>

   <insert id="insertDept" parameterType="Dept">

      insert into dept(dept_name,dept_address) values(#{deptName},#{deptAddress});

   </insert>


</mapper>

 

myBatis-config.xml文件修改後爲以下內容ajax

<?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>

<!-- 經過別名簡化對類的使用 -->

<typeAliases>

      <typeAlias type="cn.itcast.scm.entity.Dept" alias="Dept" />

</typeAliases>

   <mappers>

      <mapper resource="cn/itcast/scm/entity/DeptMapper.xml" />

   </mappers>     

</configuration>

 

1.6編寫Dao接口及實現

DeptDaoImpl.javaspring

@Repository("deptDao")

public class DeptDaoImpl{

  
   @Resource

   private SqlSessionTemplate sqlSessionTemplate;

  

   /**

    * 根據部門編號查詢部門信息

    * @param deptId 部門編號

    * @return 部門信息

    */

   public Dept selectDept(Integer deptId){

      Dept dept=   sqlSessionTemplate.selectOne("cn.itcast.entity.DeptMapper.selectDept", deptId);       

      return dept;

   }

   /**

    * 添加部門信息

    * @param dept 部門信息

    * @return 添加成功的記錄數

    */

   public int insertDept(Dept dept){

         System.out.println("------dao.dept:"+dept);

         return sqlSessionTemplate.insert("cn.itcast.entity.DeptMapper.insertDept", dept);      

   }

}

 

1.7測試spring與mybatis整合

public class TestDeptDao {

 

   //@Resource //這裏無法使用,後繼版本有其它方式能夠注入

   static private DeptDaoImpl deptDao;

   @BeforeClass

   public static void setUpBeforeClass() throws Exception {

      ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext.xml");

      deptDao=(DeptDaoImpl) context.getBean("deptDao");

   }

 

   @AfterClass

   public static void tearDownAfterClass() throws Exception {

   }

 

   @Test

   public void testSelectDept() {

      System.out.println(deptDao.selectDept(1));

   }

 

   @Test

   public void testInsertDept() {

      Dept dept=new Dept();

      //dept.setDeptId(117);

      dept.setDeptName("name117");

      dept.setDeptAddress("address117");

      System.out.println("受影響行數:"+deptDao.insertDept(dept));

   }

}

 

 

1.8整合springmvc

修改web.xml文件,加入springmvc相關信息,編寫 控制器類及相關jsp 文件

 

  spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"

   xmlns:context="http://www.springframework.org/schema/context"

   xmlns:p="http://www.springframework.org/schema/p"

   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

   http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

   ">

        

   <mvc:annotation-driven></mvc:annotation-driven>

   <context:component-scan base-package="*"/>

</beans>

 

 web.xml文件配置

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"

   xmlns="http://java.sun.com/xml/ns/javaee"

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

   http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

  <display-name></display-name>

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

   </listener>

   <context-param>

      <param-name>contextConfigLocation</param-name>

      <param-value>classpath:applicationContext.xml</param-value>

   </context-param>

  <servlet>

      <servlet-name>mvc</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

      <init-param>

         <param-name>contextConfigLocation</param-name>

         <param-value>classpath:spring-mvc.xml</param-value>

      </init-param>

      <load-on-startup>1</load-on-startup>

   </servlet>

   <servlet-mapping>

      <servlet-name>mvc</servlet-name>

      <url-pattern>*.action</url-pattern>

   </servlet-mapping>

   <filter>

      <filter-name>encodingFilter</filter-name>

   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <init-param>

         <param-name>encoding</param-name>

         <param-value>UTF-8</param-value>

      </init-param>

   </filter>

   <filter-mapping>

      <filter-name>encodingFilter</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

  

</web-app>

 

 編寫控制器類

@Controller

@RequestMapping(value="/dept")

public class DeptAction {

   @Resource

   private DeptDaoImpl deptDao;

  

   @RequestMapping(value="/insert")

   public String insert(Dept dept){

      System.out.println("---action.dept:"+dept);

      deptDao.insertDept(dept);

      return "forward:/jsp/main.jsp";

   }

}

 

  縮寫跳轉頁面

/jsp/main.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

<head>

</head>

<body>

 this is main jsp

</body>

</html>

 

1.9測試ssm整合

縮寫測試頁面

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<html>

<head>

</head>

<body>

   <form action="dept/insert.action" method="post">

      名稱:<input type="text"  name="deptName"><br>

      地址:<input type="text" name="deptAddress"><br>

      <input type="submit" value="ok">

   </form>

</body>

</html>

 

 

2、ssm框架優化

2.1中文亂碼

中文亂碼處理,在web.xml中配置攔截器(參考前面)

<filter>

      <filter-name>encodingFilter</filter-name>

   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

      <init-param>

         <param-name>encoding</param-name>

         <param-value>UTF-8</param-value>

      </init-param>

   </filter>

   <filter-mapping>

      <filter-name>encodingFilter</filter-name>

      <url-pattern>/*</url-pattern>

   </filter-mapping>

 

2.2添加業務層

1).添加業務層相關包、接口及實現

接口包:cn.itcast.service

實現類包:cn.itcast.service.impl

編寫接口與實現類(實現類用@Service進行註解,dao接口結合下邊的配置,經過@Autowired方式注入代理實例),略。

 

2.3添加dao層接口

2.4修改applicationContext.xml與spring-mvc.xml文件

添加以下內容:

<!-- 把事務邊界定在service層 -->

   <aop:config>

      <aop:advisor advice-ref="advice" pointcut="execution(* cn.itcast.scm.service.impl.*.*(..))"/>

   </aop:config>

<!-- 自動掃描組件,要把controller去除,他們是在spring-mvc.xml中配置,若是不去除會影響事務管理。   -->

      <context:component-scan base-package="cn.itcast">

      <context:exclude-filter type="annotation"         expression="org.springframework.stereotype.Controller" />

   </context:component-scan>  

  

      <!-- 配置 轉換器,對於在basePackage設置的包(包括子包)下的接口類,若是在Mapper.xml文件中定義過,

   將被轉換成spring的BEAN,在調用 的地方經過@Autowired方式將能夠注入接口實例-->

  

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

      <property name="sqlSessionFactory" ref="sqlSessionFactory"/>

      <property name="basePackage" value="cn.itcast.scm.dao"/>

   </bean>

 

spring-mvc.xml

<!-- 掃描全部的controller 可是不掃描service -->

   <context:component-scan base-package="cn.itcast">

      <context:include-filter type="annotation"

         expression="org.springframework.stereotype.Controller" />

      <context:exclude-filter type="annotation"

         expression="org.springframework.stereotype.Service" />

   </context:component-scan>

 

2.5修改各層的調用

控制器類經過業務層接口調用業務層,業務層再經過dao接口(可刪除dao實現類,及測試類)獲取代理對象執行相關SQL,進行數據的操做

 

3、開發環境與插件

3.1軟件、框架版本約定

編號

工具

版本

說明

  1.  

MyBatis

3.2

實現持久化操做

  1.  

Spring

3.2

用於分層解藕

  1.  

Junit

4.0

單元測試

  1.  

JQuery

2.0 (支持IE 10 和以上的版本)

實現ajax各類特效和插件

  1.  

EasyUI

1.3.5

基於jQuery的用戶界面插件

  1.  

SVN

1.6

開發版本控制軟件

  1.  

sitemesh

2.4.2

網頁佈局和修飾的框架

  1.  

Ztree

3.5.26

樹狀數據的web顯示

3.2開發環境

操做系統

Windows 7

 

開發工具

myEclipse 10

 

數據庫

mysql-5.5

 

Web容器

Tomcat 7 / Tomcat6

 

JDK

JDK 1.6+ J2EE 6.0 + Tomcat 7.0  (開發環境與部署環境相同)

JDK 1.5 + J2EE 5.0 + Tomcat 6.0 (開發環境與部署環境相同)

3.3 SVN介紹

1: CVS: 版本控制器鼻祖     2: SVN集中式版本控制器    3: git分佈式版本控制器

2: svn安裝工具

3: 建立倉庫: svnadmin create "e:\testdir\student_svn"   

3.1: 經過客戶端選定要提交的文件夾import 信息提交到倉庫(倉庫URLsvn://localhost:3690)的時候會出現: "目標積極拒絕,沒法鏈接" 說明服務是沒有啓動

3.2: 啓動服務:  svnserve -d -r e:\testdir\student_svn     上傳項目的時候會提示: 認證失敗,這是因爲沒有設置用戶名與密碼

3.3: 修改E:\testdir\student_svn\conf目錄下的svnserve.conf及pwsswd文件,svnserve.conf文件中開啓密碼(去掉前面的註釋包括空格): password-db = passwd,pwsswd文件添加用戶名及密碼格式:用戶名=密碼

3.4: 經過dos命令建立的服務,命令行窗口是不可以關閉的(關閉窗口,服務也關閉), -d 此參數是僅僅在Linux下面有效的

4: import:  第一次提交,對於同一個項目import只能使用一次,之後都是在原來版本下 commit

5: checkout: 從倉庫中下載指定的版本,默認下載是最新版 (在下載的項目中有隱藏 "svn"文件夾)此文件夾中記錄服務器相關信息

6: export: 從倉庫中下載指定的版本,默認下載是最新版,可是下載完畢以後與SVN倉庫沒有任何聯繫,之後也不能進行版本的更新

7:update: 能夠更新倉庫中新版本,默認是最新版

8: revert: 能夠指定某些文件還原到下載的初始版本

9:commit: 本地的工做副本提交到倉庫中

10: 在myEclipse中配置svn插件 svn.link 文件中配置: path=soft\\svn,啓動myelcipse便可使用SVN功能

插件安裝與使用參考:

1)解壓zip文件

2)把features,pougins文件夾copy到C:\Users\chufeng\MyEclipse\MyEclipse 10\soft\svn目錄下(C:\Users\chufeng\MyEclipse\MyEclipse 10\爲myeclipse安裝目錄, soft/svn能夠本身建立)

3)進入C:\Users\chufeng\MyEclipse\MyEclipse 10\dropins目錄,並新建svn.link文件,添加內容:path=soft\svn

4)啓動myeclipse

5)選擇myeclipse的「file"-->import-->svn-->從svn中檢出-->檢出時,須要填寫URL地址,URL爲:svn://IP地址[:端口號],使用默認的端口時能夠不用寫端口

3.4簡化實體別名及mapper.xml文件配置

修改applicationContext.xml

<!-- 配置session工廠 -->

   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

      <property name="dataSource" ref="dataSource" />

      <property name="configLocation" value="classpath:myBatis-config.xml" />

      <!--配置掃描式加載SQL映射文件,記得去掉mybatis-config配置-->

      <property name="mapperLocations" value="classpath:cn/itcast/scm/dao/*.xml"/>   

   </bean>

 

mybatis-config.xml添加支持基於包的別名掃描

<!-- 經過別名簡化對類的使用 -->

<typeAliases>

<!-- <typeAlias type="cn.itcast.scm.entity.Dept" alias="Dept" />    -->

      <!--

      經過package, 能夠直接指定package的名字, mybatis會自動掃描你指定包下面的javabean,

      而且默認設置一個別名,默認的名字爲: javabean 的首字母小寫的非限定類名來做爲它的別名。   

       -->

      <package name="cn.itcast.scm.entity"/>

     

</typeAliases>

 

3.5 mybatis逆向工程代碼生成器(插件)

--安裝插件:

1。解壓mybatis_generator_1.3.1.zip文件

2。把features,pougins文件夾copy到C:\Users\chufeng\MyEclipse\MyEclipse 10\soft\mybatis目錄下(C:\Users\chufeng\MyEclipse\MyEclipse 10\爲myeclipse安裝目錄, soft\mybaits能夠本身建立)

3。進入C:\Users\chufeng\MyEclipse\MyEclipse 10\dropins目錄,並新建mybatis.link文件,添加內容:path=C:\\Users\\chufeng\\MyEclipse\\MyEclipse 10\\soft\\mybatis

4。啓動myeclipse

--使用插件

5。項目中添加generatorConfig.xml文件,並修改相關內容。右建能夠找到generator mybatis artifacts生成

mybatis逆向工程代碼生成器(插件)百度雲連接: http://pan.baidu.com/s/1hr6NefE

 

generatorConfig.xml文件內容:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE generatorConfiguration

  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"

  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

 

<generatorConfiguration>

     <!--

   處理1

     -->

    <classPathEntry location="E:\tools\lib\mysql_driver\mysql-connector-java-5.1.26-bin.jar"/>

   <!-- 指定運行環境是mybatis3的版本 -->

   <context id="testTables" targetRuntime="MyBatis3">

 

      <commentGenerator>

      <!-- 是否取消註釋 -->

         <property name="suppressAllComments" value="true" />

          <!-- 是否生成註釋代時間戳 -->

         <property name="suppressDate" value="true" />

      </commentGenerator>

      <!-- 處理2  jdbc 鏈接信息 -->

      <jdbcConnection driverClass="com.mysql.jdbc.Driver"

         connectionURL="jdbc:mysql://localhost:3306/scm?useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="root">

      </jdbcConnection>

     

        <!--處理3 targetPackage指定模型在生成在哪一個包 ,targetProject指定項目的src,-->       

      <javaModelGenerator targetPackage="cn.itcast.scm.entity"

         targetProject="scm/src">

         <!-- 去除字段先後空格 -->

         <property name="trimStrings" value="true" />

      </javaModelGenerator>

      <!-- 處理4 配置SQL映射文件生成信息 -->

      <sqlMapGenerator targetPackage="cn.itcast.scm.dao"

         targetProject="scm/src" />

      <!--處理5 配置dao接口生成信息-->

      <javaClientGenerator type="XMLMAPPER" targetPackage="cn.itcast.scm.dao" targetProject="scm/src" />

 

   <!--處理6 指定表及實體類的映射-->

      <table tableName="account" domainObjectName="Account"

         enableSelectByExample="true" enableDeleteByExample="true"

         enableCountByExample="true" enableUpdateByExample="true"

         enableInsert="true" />

        

      <table tableName="supplier" domainObjectName="Supplier"

         enableSelectByExample="true" enableDeleteByExample="true"

         enableCountByExample="true" enableUpdateByExample="true"

         enableInsert="true" />

   </context>

</generatorConfiguration>

注意:

1.完成後記得把實體實現Serializable,重寫一下toString()方法,方便之後使用。

2.當從新使用generatorConfig.xml生成代碼時,會在已經存在的數據庫後面繼續追加代碼而非覆蓋致使出現大量的重複代碼,一個聰明的辦法是生成前先註釋掉已經生成好的數據庫。

 

3.6 jquery與easyUI技術整合

jquery-easyui-1.3.5版本,解壓後文件夾直接copy到項目webroot虛擬目錄下,文件夾說明:

demo:能夠查閱例子,在項目中能夠刪除

local:本地化資源,通常保留本地化文件

plugins:部署用,

src:源碼,分析用,能夠刪除

thems:樣式、圖標等信息

導入easyUI相關的文件

 

編寫測試頁面easyUI.jsp:

 

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

 

<c:set var="proPath" value="${pageContext.request.contextPath}" />

<link rel="stylesheet" type="text/css"

   href="${proPath}/jquery-easyui-1.3.5/themes/default/easyui.css">

<link rel="stylesheet" type="text/css" href="${proPath}/jquery-easyui-1.3.5/themes/icon.css">

<script type="text/javascript" src="${proPath}/jquery-easyui-1.3.5/jquery.min.js"></script>

<script type="text/javascript" src="${proPath}/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>

 

<title>My JSP 'MyJsp.jsp' starting page</title>

 

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

   <link rel="stylesheet" type="text/css" href="styles.css">

   -->

<script type="text/javascript">

$(function(){alert("可使用了!");});

</script>

</head>

 

<body>

   <div id="p" class="easyui-panel" style="width:500px;height:200px;padding:10px;"  

        title="My Panel" iconCls="icon-save" collapsible="true">  

    test my panel   

</div>

</body>

</html>

 

瀏覽器輸入地址訪問,並觀察是否正常!

注意:

1.jquery.min.js 報錯(有紅點的問題):解決方式爲選中 :項目文件夾 -> 鼠標右鍵 -> MyEclipse -> manage validation -> 左面點擊 -> Excluded resources -> 找到jquery.min.js -> 打上鉤 -> apply

2.瀏覽器:建議使用IE10或以上版本、火狐

 

3.7 json使用

導入包

spring-mvc.xml配置支持JSON註解

<mvc:annotation-driven></mvc:annotation-driven>

 

action中添加支持json交互方法:

   @RequestMapping("/doAjax")

   @ResponseBody //若是返回json格式,須要這個註解,這裏用來測試環境

   public Object doAjax(Supplier supplier){

      System.out.println("---doAjax.supplier:"+supplier);

      supplier.setSupName("supName1");

      return supplier;

   }

 

 

編寫測試頁面:json.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

 

<c:set var="proPath" value="${pageContext.request.contextPath}" />

<link rel="stylesheet" type="text/css"

   href="${proPath}/jquery-easyui-1.3.5/themes/default/easyui.css">

<link rel="stylesheet" type="text/css" href="${proPath}/jquery-easyui-1.3.5/themes/icon.css">

<script type="text/javascript" src="${proPath}/jquery-easyui-1.3.5/jquery.min.js"></script>

<script type="text/javascript" src="${proPath}/jquery-easyui-1.3.5/jquery.easyui.min.js"></script>

 

 

    <title>My JSP 'json.jsp' starting page</title>

  <script type="text/javascript">

      $(

         function(){

             $("#bt1").click(

                function(){

                   $.post(

                      "supplier/doAjax.action",

                      {supId:1001,supName:"name1001"},

                      function(json){alert(json.supId+"||"+json.supName);},

                      "json"

                   );

                }

             );          

         }

      );

 

  </script>

 

  </head> 

  <body>

   <button id="bt1" >testJson</button>

  </body>

</html>

 

 4、Java Web框架對比:SSH和SSM

4.1 SSH:Spring+Struts2+Hibernate SSM:Spring+SpringMVC+MyBaitis

   兩個框架都是IoC容器+MVC框架+ORM框架。IoC的意思是控制反轉,意思是把建立和查找依賴對象的控制權交給容器而不是本身實例化對象;MVC框架採用MVC分層,模型層處理數據邏輯,一般是模型對象在數據庫存取數據,視圖層處理數據顯示,控制器層處理用戶交互,一般從視圖讀取數據,控制用戶輸入,並向模型發送數據;ORM框架即對象-關係映射模型,在數據庫表和持久化對象間進行映射,這樣在操做數據庫時只須要對對象操做。

 

4.2 Spring

   在IoC容器方面,SSH和SSM都是使用Spring。Spring是輕量級的IoC和AOP容器。IoC容器是Spring的核心,負責建立對象,管理對象,裝配對象,配置對象,而且管理這些對象的整個生命週期。管理對象動態向某個對象提供其餘對象,經過依賴注入來實現,Spring有三種注入方式:接口注入、Set注入和構造注入。Spring AOP即面向切面編程,能夠用在日誌和事務管理等方面。

 

4.3 Struts2與SpringMVC

   MVC整合框架二者分別使用的是Struts2和SpringMVC。二者的區別:1.Struts2是類級別的攔截, 一個類對應一個request上下文,SpringMVC是方法級別的攔截,一個方法對應一個request上下文,所以容易實現restful API;2.Struts2是多例的,每次請求都建立一個Action,類屬性被方法共享,而SpringMVC是單例的,只有一個實例,方法之間變量不共享;3.Struts2的核心控制器是Filter,SpringMVC的核心控制器是Servlet;4.攔截器方面,Struts2有本身的interceptor機制,SpringMVC用的是獨立的AOP方式;5.SpringMVC是Spring的一個模塊,項目管理和安全性比Struts2好,配置文件少,而且集成了Ajax,處理ajax請求,直接經過返回數據,方法中使用註解@ResponseBody,能自動將對象轉換爲JSON數據。

 

4.4 Hibernate與MyBaitis

   ORM框架分別用的是Hibernate和MyBaitis。MyBatis的sql語句是手動編寫的,能夠進行更爲細緻的SQL優化,能夠減小查詢字段,具備高度靈活,可優化,易維護的特色。但須要維護SQL和結果映射,工做量大。Hibernate面向對象查詢,以對象化的思惟操做數據庫,hql與具體的數據庫無關,移植性更好。Hibernate無需編寫SQL,映射的規則也能夠由IDE生成,提升了開發效率,還提供了緩存、日誌、級聯等強大功能。可是對於多表關聯複雜SQL、數據系統權限限制、根據條件編寫SQL、存儲過程等十分不便,性能難以經過SQL優化。

相關文章
相關標籤/搜索