ssh整合思想 Spring與Hibernate和Struts2的action整合 調用action添加資料庫 使用HibernateTemplate的save(entity)方法 ognl.MethodFailedException:Method"execute" failed for ssh包合集下載

来源:https://www.cnblogs.com/qingyundian/archive/2018/01/03/8185138.html
-Advertisement-
Play Games

自動調用Spring的bean.xml配置文件 需要web.xml啟動文件 代碼如下: 其中調用了過濾器和監聽器 Spring核心配置文件bean.xml代碼 配置文件註入對象屬性,註意需要類當中聲明屬性並設置setter方法 層層調用 UserAction類代碼如下: UserService類代碼 ...


自動調用Spring的bean.xml配置文件

需要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>2018-01-03_Spring_Hibernate</display-name>
  
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:bean.xml</param-value>
  </context-param>
  
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <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>
</web-app>

其中調用了過濾器和監聽器

Spring核心配置文件bean.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: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/context http://www.springframework.org/schema/context/spring-context.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">

    <!-- c3p0連接池得到dataSource -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sw_database"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
    
    <bean id="userAction" class="com.swift.action.UserAction" scope="prototype">
    <property name="userService" ref="userService"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <!-- Hibernate核心配置文件沒有連接資料庫,所以需要註入 -->
    <property name="dataSource" ref="dataSource"></property>
    <!-- Hibernate核心配置文件的位置 -->
    <property name="configLocations" value="classpath:hibernate.cfg.xml"></property>
    </bean>
    
    <bean id="userService" class="com.swift.service.UserService">
    <property name="userDao" ref="userDaoImplements"></property>
    </bean>
    
    <bean id="userDaoImplements" class="com.swift.dao.UserDaoImplements">
    <property name="hibernateTemplate" ref="hibernateTemplate"></property>
    </bean>
    
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
       <!-- 前一個sessionFactory是HibernateTemplate類內部的 -->
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    

</beans>

配置文件註入對象屬性,註意需要類當中聲明屬性並設置setter方法

層層調用

UserAction類代碼如下:

package com.swift.action;

import com.opensymphony.xwork2.ActionSupport;
import com.swift.service.UserService;

public class UserAction extends ActionSupport {
    private UserService userService;
    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    @Override
    public String execute() throws Exception {
        System.out.println("action..................");
        userService.add();
        return NONE;
    }

}

UserService類代碼如下:

package com.swift.service;

import com.swift.dao.UserDao;

public class UserService {

    private UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    public void add() {
        System.out.println("UserService.................");
        userDao.add();
    }
    
}

UserDao介面代碼如下:

 package com.swift.dao; public interface UserDao { public void add(); } 

UserDao介面的實現類UserDaoImplement代碼如下:

package com.swift.dao;

import org.springframework.orm.hibernate5.HibernateTemplate;

import com.swift.entity.User;

public class UserDaoImplements implements UserDao{
    private HibernateTemplate hibernateTemplate;
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @Override
    public void add() {
        User user=new User();
        user.setUsername("small-fly");
        user.setAddress("War of Mercenaries");
        hibernateTemplate.save(user);
    }

}

hibernate核心配置文件hibernate.cfg.xml文件代碼

<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC   
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"   
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/sw_database</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property> -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</property>
        
        
        <property name="hibernate.show_sql">true</property>

        <!-- create: 先刪表,再建表。 create-drop: 啟動時建表,退出前刪表。 update: 如果表結構不一致,就創建或更新。 
            validate: 啟動時驗證表結構,如果不致就拋異常。 -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        

        <!--指定映射文件,可映射多個映射文件 -->
        <mapping resource="User.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

hibernate映射文件代碼如下:

<?xml version="1.0" encoding='UTF-8'?>     
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 實體類映射文件 -->
<hibernate-mapping>

    <class name="com.swift.entity.User" table="t_user">
        <!-- 主鍵 -->
        <id name="uid">
            <generator class="native"></generator>
        </id>
        <!-- 其他屬性 -->
        <property name="username"/>
        <property name="address"/>
    </class>
    
</hibernate-mapping>

運行action出現問題

原因,jar包問題,重新整理OK的SSH jar包合集

下載包地址:

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一.nullptr 示例: #include void foo(char * c){}void foo(int n){}int main(){foo(0);// foo(NULL); // ... ...
  • 1.Array & Arrays 與Collection & Collections區別 (1)Collection": 是一個介面,與其子類共同組成一個Collection集合框架; Collections: 是一個類,一個服務於集合的工具類, 提供一系列靜態方法實現對各種集合的搜索、排序、線程安 ...
  • 文章結尾有彩蛋 《學生成績管理系統》實習內容及要求 一、項目名稱:“學生成績管理系統”,包名是“班級名稱全拼”(例如計算171),Java文件名稱是“姓名全拼+班級號+學號”(例如計算171班的張三,學號為5號,則他的文件名稱為“zhangsan1705),其他類名稱為“類名+班級號+學號”(例如計 ...
  • 兩種最重要的標準庫 string和vector string和vector是兩種最重要的標準庫類型,string表示可變長的字元序列,vector存放的是某種給定類型對象的可變長序列。 一、標準庫類型string 1.定義和初始化string對象:初始化string對象的方式有 string s1 ...
  • Python是一種解釋型、面向對象、動態數據類型的高級程式設計語言。 像Perl語言一樣,Python源代碼同樣遵循GPL(GUN General Public License)協議。 我目前學習的是Python2.x版本。 接下來我來說一說Python的特點 1.易於學習:有相對較少的關鍵字,結構 ...
  • 1.Flask: Flask是一個基於Python開發並且依賴jinja2模板和Werkzeug WSGI服務的一個微型框架,對於Werkzeug本質是Socket服務端,其用於接收http請求並對請求進行預處理,然後觸發Flask框架,開發人員基於Flask框架提供的功能對請求進行相應的處理,並返 ...
  • public static void main(String[] args) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 10; j++) { System.out.print(" "); } for (int j = 0; j <5-i; ...
  • 1.當使用轉發時,JSP容器將使用一個內部方法來調用目標頁面,新的頁面繼續處理同一個請求,而瀏覽器不會知道這個過程; 2.重定向是第一個頁面通知瀏覽器發送一個新的頁面請求. 3.轉發不改變URL,重定向回改變URL; 4.因為瀏覽器要發出新請求,故而重定向慢一些; 5.由於發生了新請求,故而重定向之 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...