锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
]]>
]]>
鍓嶉潰閫氳繃PlugIn鎴戜滑瀹炵幇浜哠pring Context鐨勫姞杞斤紝涓嶈繃浠呬粎鍔犺澆Context騫舵病鏈変粈涔堝疄闄?
鎰忎箟錛屾垜浠繕闇瑕佷慨鏀歸厤緗紝灝哠truts Action浜ょ粰Spring瀹瑰櫒榪涜綆$悊銆備笅闈㈠皢閫氳繃涓涓猂egsit
瀹炰緥鍔犱互璇存槑銆?
棣栧厛鍑嗗濂絩egist.jsp鍜宭ogin.jsp,鍦╮egist.jsp涓?鐨刦orm鏈塶ame,password,password2,email鍩熴?
鍦╯truts-config.xml涓坊鍔犻厤緗細
<form-bean name="registForm" type="com.lzy.forum.form.RegistForm" />
<action path="/regist" name="registForm"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="true" input="/regist.jsp" scope="request">
<forward name="failure" path="/regist.jsp" />
<forward name="success" path="/login.jsp" />
</action>
RegistForm,RegistAction鎸夌収鍘熸潵Struts鐨勬柟娉曞幓鍐?鎴戝湪RegistAction涓姞鍏ヤ簡涓涓猆serDAO瀵硅薄
userDAO錛岃繖涓璞$敱Spring娉ㄥ叆銆?
<bean name="/regist" class="com.lzy.forum.action.RegistAction"
singleton="false">
<property name="userDAO">
<ref local="userDAO" />
</property>
</bean>
鍩烘湰涓奡SH鐨勬灦鏋勫凡緇忓畬鎴愶紝緇忕‘璁ゅ悗鍙互鑷繁嫻嬭瘯涓涓嬩簡銆傦紙瀹岋級
]]>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
聽聽聽聽聽聽<set-property property="contextConfigLocation"
聽聽聽聽聽聽聽聽聽聽 聽value="/WEB-INF/action-servlet.xml" />
聽</plug-in>
WEB-INF鐩綍涓嬬殑action-servlet.xml灝辨槸Spring鐨勯厤緗枃浠?涓嬮潰緇欏嚭鐨勬槸鏁翠釜鐨?/font>action-servlet.xml鏂囦歡錛?/font>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "<beans>
//Data Source
聽<bean id="dataSource"
聽聽class="org.springframework.jdbc.datasource.DriverManagerDataSource">
聽聽<property name="driverClassName">
聽聽聽<value>com.mysql.jdbc.Driver</value>
聽聽</property>
聽聽<property name="url">
聽聽聽<value>jdbc:mysql://localhost:3306/forum</value>
聽聽</property>
聽聽<property name="username">
聽聽聽<value>test</value>
聽聽</property>
聽聽<property name="password">
聽聽聽<null />
聽聽</property>
聽</bean>
// For Hibernate
聽<bean id="sessionFactory"
聽聽class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
聽聽<property name="dataSource">
聽聽聽<ref local="dataSource" />
聽聽</property>
聽聽<property name="mappingResources">
聽聽聽<list>
聽聽聽聽<value>com/lzy/forum/domain/User.hbm.xml</value>
聽聽聽聽<value>com/lzy/forum/domain/Article.hbm.xml</value>
聽聽聽聽<value>com/lzy/forum/domain/Board.hbm.xml</value>
聽聽聽</list>
聽聽</property>
聽聽<property name="hibernateProperties">
聽聽聽<props>
聽聽聽聽<prop key="hibernate.dialect">
聽聽聽聽聽org.hibernate.dialect.MySQLDialect
聽聽聽聽</prop>
聽聽聽聽<prop key="hibernate.show_sql">true</prop>
聽聽聽</props>
聽聽</property>
聽</bean>
聽<bean id="transactionManager"
聽聽class="org.springframework.orm.hibernate3.HibernateTransactionManager">
聽聽<property name="sessionFactory">
聽聽聽<ref local="sessionFactory" />
聽聽</property>
聽</bean>
聽
// For DAO
聽<bean id="userDAO"
聽聽class="com.lzy.forum.dao.impl.UserDAOHibernate">
聽聽<property name="sessionFactory">
聽聽聽<ref local="sessionFactory" />
聽聽</property>
聽</bean>
聽
聽<bean id="boardDAO"
聽聽class="com.lzy.forum.dao.impl.BoardDAOHibernate">
聽聽<property name="sessionFactory">
聽聽聽<ref local="sessionFactory" />
聽聽</property>
聽</bean>
聽<bean id="articleDAO"
聽聽class="com.lzy.forum.dao.impl.ArticleDAOHibernate">
聽聽<property name="sessionFactory">
聽聽聽<ref local="sessionFactory" />
聽聽</property>
聽</bean>
//For Struts Action
聽<bean name="/regist" class="com.lzy.forum.action.RegistAction"
聽聽singleton="false">
聽聽<property name="userDAO">
聽聽聽<ref local="userDAO" />
聽聽</property>
聽</bean>
聽<bean name="/login" class="com.lzy.forum.action.LoginAction"
聽聽singleton="false">
聽聽<property name="userDAO">
聽聽聽<ref local="userDAO" />
聽聽</property>
聽</bean>
聽<bean name="/boardManage"
聽聽class="com.lzy.forum.action.BoardManageAction" singleton="false">
聽聽<property name="userDAO">
聽聽聽<ref local="userDAO" />
聽聽</property>
聽聽<property name="boardDAO">
聽聽聽<ref local="boardDAO" />
聽聽</property>
聽</bean>
聽<bean name="/boardNavigate"
聽聽class="com.lzy.forum.action.BoardNavigateAction" singleton="false">
聽聽<property name="boardDAO">
聽聽聽<ref local="boardDAO" />
聽聽</property>
聽</bean>
聽<bean name="/boardDisplay"
聽聽class="com.lzy.forum.action.BoardDisplayAction" singleton="false">
聽聽<property name="boardDAO">
聽聽聽<ref local="boardDAO" />
聽聽</property>
聽</bean>
聽<bean name="/articleManage"
聽聽class="com.lzy.forum.action.ArticleManageAction" singleton="false">
聽聽<property name="userDAO">
聽聽聽<ref local="userDAO" />
聽聽</property>
聽聽<property name="boardDAO">
聽聽聽<ref local="boardDAO" />
聽聽</property>
聽聽<property name="articleDAO">
聽聽聽<ref local="articleDAO" />
聽聽</property>
聽</bean>
</beans>
緇忚繃榪欐牱鐨勯厤緗箣鍚庯紝Hibernate浜嬪姟浜ょ敱Spring綆$悊錛宻essionFactory浠嶴pring瀹瑰櫒鑾峰緱銆備笅闈㈤氳繃鍏朵腑鐨勪竴涓狣AO瀹炰緥鏉ョ湅鐪嬭繖鏍峰仛甯︽潵鐨勬柟渚褲?br />
//BoardDAO.java
package com.lzy.forum.dao;
import java.util.ArrayList;
import com.lzy.forum.domain.Board;
public interface BoardDAO {
聽
聽public boolean isBoardExist(String name);
聽
聽public void addBoard(Board board);
聽
聽public void deleteBoard(Board board);
聽
聽public Board loadBoard(int id);
聽
聽public Board loadBoard(String name);
聽
聽public Board loadBoardWithArticles(int id);
聽
聽public ArrayList getRootBoardsList();
聽
聽
聽
}
//BoardDAOHibernate.java
package com.lzy.forum.dao.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Set;
import org.hibernate.Hibernate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lzy.forum.dao.BoardDAO;
import com.lzy.forum.domain.Board;
public class BoardDAOHibernate extends HibernateDaoSupport implements BoardDAO {
聽public void deleteBoard(Board board) {
聽聽// TODO Auto-generated method stub
聽聽this.getHibernateTemplate().delete(board);
聽}
聽public void addBoard(Board board) {
聽聽// TODO Auto-generated method stub
聽聽this.getHibernateTemplate().saveOrUpdate(board);
聽}
聽public boolean isBoardExist(String name) {
聽聽// TODO Auto-generated method stub
聽聽ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board where name = ? ", new Object[] { name });
聽聽if (list.size() > 0) {
聽聽聽// System.out.println("find it");
聽聽聽return true;
聽聽}
聽聽return false;
聽}
聽public Board loadBoard(int id) {
聽聽// TODO Auto-generated method stub
聽聽ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board where id = ? ", new Object[] { id });
聽聽Board b = null;
聽聽if (list.size() > 0) {
聽聽聽b = (Board) list.get(0);
聽聽}
聽聽return b;
聽}
聽public Board loadBoard(String name) {
聽聽// TODO Auto-generated method stub
聽聽ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board where name = ? ", new Object[] { name });
聽聽Board b = null;
聽聽if (list.size() > 0) {
聽聽聽b = (Board) list.get(0);
聽聽}
聽聽return b;
聽}
聽public ArrayList getRootBoardsList() {
聽聽// TODO Auto-generated method stub
聽聽ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board b left join fetch b.ChildBoards where b.Parent = null order by b.id");
聽聽System.out.println(list.size() + " root boards found ");
聽聽
聽聽ListIterator index = list.listIterator();
聽聽while (index.hasNext()) {
聽聽聽Board s = (Board) index.next();
聽聽聽//this.getSession(true);
聽聽聽//Hibernate.initialize(s.getChildBoards());
聽聽聽///*
聽聽聽 Set children聽 = (Set) s.getChildBoards();
聽聽聽 Iterator it = children.iterator();
聽聽聽
聽聽聽 while(it.hasNext()){
聽聽聽 Board b = (Board)it.next();
聽聽聽 //Hibernate.initialize(b);
聽聽聽 System.out.println(b.getName());
聽聽聽 }
聽聽聽 //*/
聽聽聽//System.out.println(children.size() + "child(ren) found ");
聽聽聽//s.setChildBoards(s.getChildBoards());
聽聽}
聽聽return list;
聽}
聽public Board loadBoardWithArticles(int id) {
聽聽// TODO Auto-generated method stub
聽聽ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board b left join fetch b.Articles where b.Id = ? ", new Object[] { id });
聽聽Board b = null;
聽聽if (list.size() > 0) {
聽聽聽b = (Board) list.get(0);
聽聽}
聽聽return b;
聽}
}
BoardDAOHibernate緇ф壙 HibernateDaoSupport鍚庯紝閫氳繃getHibernateTemplate()寰楀埌涓涓狧ibernateTemplate瀹炰緥錛岀劧鍚庢墽琛孋RUD鎿嶄綔錛岄潪甯哥畝鍗曘傞渶瑕佹敞鎰忕殑鏄紝鐢變簬鎵ц涓嬈RUD鎿嶄綔鍚庯紝Hibernate session鍏抽棴錛屽鏋滄湁浣跨敤寤惰繜鍔犺澆絳栫暐鐨勫璞℃病鏈夊姞杞斤紝鍦ㄥ悗闈㈢殑Web灞傚緢瀹規槗鍑虹幇濡備笅閿欒錛?br />failed to lazily initialize a collection of role
浠庣綉涓婃悳鍒扮殑瑙e喅鏂規硶鏄疧pen Session In View錛?br /><filter>
聽聽聽聽 <filter-name>hibernateFilter</filter-name>
聽聽聽聽 <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
聽聽聽聽 </filter-class>
聽聽聽聽 <!-- singleSession榛樿涓簍rue,鑻ヨ涓篺alse鍒欑瓑浜庢病鐢∣penSessionInView -->
聽聽聽 <init-param>
聽聽聽聽聽聽 <param-name>singleSession</param-name>
聽聽聽聽聽聽 <param-value>true</param-value>
聽聽聽 </init-param>
聽 </filter>
聽聽 <filter-mapping>
聽聽聽聽 <filter-name>hibernateFilter</filter-name>
聽聽聽聽 <url-pattern>*.do</url-pattern>
聽聽 </filter-mapping>
鎴戣瘯浜嗚瘯錛岃繕鏄湁涓浜涢棶棰橈紝鎵浠ュ悗鏉ヨ繕鏄斁寮冧簡榪欑鏂規硶錛屾墍鏈夊湪Web灞備腑闇瑕佺殑瀵硅薄閮藉繀欏誨湪Business灞備腑鍔犺澆瀹屾垚銆傚涓嬮潰鐨勪唬鐮佹墍紺猴細
ArrayList list = (ArrayList) this.getHibernateTemplate().find(
聽聽聽聽"from Board b left join fetch b.Articles where b.Id = ? ", new Object[] { id });
榪欐牱Hibernate鍜孲pring鐨勭粨鍚堜篃綆楀畬鎴愪簡銆傚叾浠栫殑涓浜汥AO綾誨拰瀹炵幇浠g爜鎴戞病鏈夌粰鍑猴紝浣嗘槸澶у悓灝忓紓錛屽拰Board鐨勫疄鐜扮被浼箋?br />
鏈鍚庡皢鏄疭truts 鍜孲pring鐨勭粨鍚堬紝灝嗗湪涓嬩竴綃囩粰鍑恒傦紙鏈畬寰呯畫錛?/font>
寮鍙戠幆澧冪殑濉斿緩鎴戝氨涓嶅璇翠簡錛屽叿浣撳彲浠ュ弬鑰冩垜浠ュ墠鐨勫嚑綃囨棩蹇椼?/p>
鍦‥clipse涓柊寤轟竴涓姩鎬乄eb宸ョ▼錛屽皢Struts 銆丼pring銆丠ibernate闇瑕佺殑jar閮芥坊鍔犲埌WEB-INF/lib鐩綍涓嬶紝
灝哠truts鏍囩鎵闇鐨則ld鏂囦歡銆乻truts-config.xml鏂囦歡錛堝彲浠ョ敱StrutsIDE鐢熸垚錛夋坊鍔犲埌WEB-INF涓嬶紝鍙﹀鍦?br />姝ょ洰褰曚笅娣誨姞涓涓猄pring鐨勯厤緗枃浠禷pplicationContext.xml銆?/p>
鎴戝弬鑰冪殑渚嬪瓙鏄鏄曠殑銆婃繁鍏ユ祬鍑篐ibernate銆嬩笂鐨勭6绔狅紝寰堝ソ鐨勪竴涓鍧涚ず渚嬨?/p>
棣栧厛鎸夌収涔︿笂鐨凟R妯″瀷錛屽湪鏁版嵁搴擄紙MySQL錛変腑寤虹珛鐩稿簲鐨勬暟鎹簱銆?/p>
create database forum;
use forum;
create table user(
聽id int not null auto_increment primary key,
聽name varchar(50),
聽pwd varchar(50),
聽email varchar(50)
)type=innodb;
create table board(
聽id int not null auto_increment primary key,聽
聽parent_id int,
聽create_by int not null,
聽name varchar(50) not null,
聽remark varchar(255),
聽create_time datetime,
聽index(parent_id),
聽index(create_by),
聽foreign key(parent_id) references board(id) on delete cascade,
聽foreign key(create_by) references user(id) on delete no action
聽
)type=innodb;
create table article(
聽id int not null auto_increment primary key,聽
聽parent_id int ,
聽board_id int not null,
聽article_type int not null,
聽title varchar(255),
聽body text,
聽create_by int not null,
聽create_time datetime,
聽hits int unsigned,
聽bytes int unsigned,
聽last_update_by int not null,
聽last_update_time datetime,
聽index(parent_id),
聽index(board_id),
聽index(create_by),
聽index(last_update_by),
聽foreign key(parent_id) references article(id) on delete no action,
聽foreign key(board_id) references board(id) on delete cascade,
聽foreign key(create_by) references user(id) on delete no action,
聽foreign key(last_update_by) references user(id) on delete no action
聽
)type=innodb;
涔嬪悗錛屾垜閫夋嫨浜嗙敤Hibernate Sychonizer鏉ョ敓鎴愭槧灝勬枃浠跺拰瀹炰綋綾伙紝鐒跺悗瀵圭敓鎴愮殑綾昏繘琛屼慨鏀癸紙鍒犳帀浜嗙敓鎴愮殑Base綾伙級錛?br />瀹為檯涓奌ibernate Sychonizer鐢熸垚鐨勬槧灝勬枃浠朵篃鏈夐敊璇紝闇瑕佷慨鏀癸紝鎴戜篃鏄悗鏉ュ湪浣跨敤涓彂鐜扮殑銆?br />
//User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
聽"-//Hibernate/Hibernate Mapping DTD//EN"
聽"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
" >
<hibernate-mapping package="com.lzy.forum.domain">
聽<class
聽聽name="User"
聽聽table="user"
聽>
聽聽<id
聽聽聽name="Id"
聽聽聽type="integer"
聽聽聽column="id"
聽聽>
聽聽聽<generator class="native"/>
聽聽</id>
聽聽<property
聽聽聽name="Name"
聽聽聽column="name"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽聽length="50"
聽聽/>
聽聽<property
聽聽聽name="Pwd"
聽聽聽column="pwd"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽聽length="50"
聽聽/>
聽聽<property
聽聽聽name="Email"
聽聽聽column="email"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽聽length="50"
聽聽/>
聽
聽聽<set name="ArticlesByLastUpdateBy" inverse="true" lazy="true">
聽聽聽<key column="last_update_by"/>
聽聽聽<one-to-many class="Article"/>
聽聽</set>
聽聽<set name="ArticlesByCreateBy" inverse="true" lazy="true">
聽聽聽<key column="create_by"/>銆?font color="#0000ff">榪欓噷鐨刢reate_by鏄悗鏉ヤ慨鏀圭殑錛孒ibernate Sychonizer鐢熸垚鐨勬湁閿欙紝鍚庨潰鏈夊嚑澶勫悓鏍風殑閿欒銆?/font>
聽聽聽<one-to-many class="Article"/>
聽聽</set>
聽聽<set name="Boards" inverse="true" lazy="true">
聽聽聽<key column="create_by"/>銆愯繖閲岀殑create_by鏄悗鏉ヤ慨鏀圭殑銆?/font>
聽聽聽<one-to-many class="Board"/>
聽聽</set>
聽</class>聽
</hibernate-mapping>
//User.java
package com.lzy.forum.domain;
public class User{
聽private static final long serialVersionUID = 1L;
//聽 primary key
聽private java.lang.Integer id;
聽// fields
聽private java.lang.String name;
聽private java.lang.String pwd;
聽private java.lang.String email;
聽// collections
聽private java.util.Set<Article> articlesByLastUpdateBy;
聽private java.util.Set<Article> articlesByCreateBy;
聽private java.util.Set<Board> boards;
聽
聽
聽public java.util.Set<Article> getArticlesByCreateBy() {
聽聽return articlesByCreateBy;
聽}
聽public void setArticlesByCreateBy(java.util.Set<Article> articlesByCreateBy) {
聽聽this.articlesByCreateBy = articlesByCreateBy;
聽}
聽public java.util.Set<Article> getArticlesByLastUpdateBy() {
聽聽return articlesByLastUpdateBy;
聽}
聽public void setArticlesByLastUpdateBy(
聽聽聽java.util.Set<Article> articlesByLastUpdateBy) {
聽聽this.articlesByLastUpdateBy = articlesByLastUpdateBy;
聽}
聽public java.util.Set<Board> getBoards() {
聽聽return boards;
聽}
聽public void setBoards(java.util.Set<Board> boards) {
聽聽this.boards = boards;
聽}
聽public java.lang.String getEmail() {
聽聽return email;
聽}
聽public void setEmail(java.lang.String email) {
聽聽this.email = email;
聽}
聽public java.lang.Integer getId() {
聽聽return id;
聽}
聽public void setId(java.lang.Integer id) {
聽聽this.id = id;
聽}
聽public java.lang.String getName() {
聽聽return name;
聽}
聽public void setName(java.lang.String name) {
聽聽this.name = name;
聽}
聽public java.lang.String getPwd() {
聽聽return pwd;
聽}
聽public void setPwd(java.lang.String pwd) {
聽聽this.pwd = pwd;
聽}
}
//Board.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
聽"-//Hibernate/Hibernate Mapping DTD//EN"
聽"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
" >
<hibernate-mapping package="com.lzy.forum.domain">
聽<class
聽聽name="Board"
聽聽table="board"
聽>
聽聽聽<meta attribute="sync-DAO">false</meta>
聽聽<id
聽聽聽name="Id"
聽聽聽type="integer"
聽聽聽column="id"
聽聽>
聽聽聽<generator class="native"/>
聽聽</id>
聽聽<property
聽聽聽name="Name"
聽聽聽column="name"
聽聽聽type="string"
聽聽聽not-null="true"
聽聽聽length="50"
聽聽/>
聽聽<property
聽聽聽name="Remark"
聽聽聽column="remark"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽聽length="255"
聽聽/>
聽聽<property
聽聽聽name="CreateTime"
聽聽聽column="create_time"
聽聽聽type="timestamp"
聽聽聽not-null="false"
聽聽聽length="19"
聽聽/>
聽聽<many-to-one
聽聽聽name="CreateBy"
聽聽聽column="create_by"
聽聽聽class="User"
聽聽聽not-null="true"
聽聽>
聽聽</many-to-one>
聽聽<many-to-one
聽聽聽name="Parent"
聽聽聽column="parent_id"
聽聽聽class="Board"聽聽聽
聽聽聽not-null="false"
聽聽聽lazy="false">
聽聽</many-to-one>
聽
聽聽<set name="Articles" inverse="true" lazy="true" order-by="id">
聽聽聽<key column="board_id"/>銆愯繖閲岀殑board_id鏄悗鏉ヤ慨鏀圭殑銆?/font>
聽聽聽<one-to-many class="Article"/>
聽聽</set>
聽聽<set name="ChildBoards" inverse="true"聽 lazy="true" order-by="id">
聽聽聽<key column="parent_id"/>銆?/font>榪欓噷鐨刾arent_id鏄悗鏉ヤ慨鏀圭殑銆?br />聽聽聽<one-to-many class="Board"/>
聽聽</set>
聽</class>聽
</hibernate-mapping>
//Board.java
package com.lzy.forum.domain;
聽
public class Board聽 {
聽private static final long serialVersionUID = 1L;
//聽 primary key
聽private java.lang.Integer id;
聽// fields
聽private java.lang.String name;
聽private java.lang.String remark;
聽private java.util.Date createTime;
聽// many to one
聽private com.lzy.forum.domain.User createBy;
聽private com.lzy.forum.domain.Board parent;
聽// collections
聽private java.util.Set<com.lzy.forum.domain.Article> articles;
聽private java.util.Set<com.lzy.forum.domain.Board> childBoards;
聽
聽
聽public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
聽聽return articles;
聽}
聽public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
聽聽this.articles = articles;
聽}
聽public java.util.Set<com.lzy.forum.domain.Board> getChildBoards() {
聽聽return childBoards;
聽}
聽public void setChildBoards(java.util.Set<com.lzy.forum.domain.Board> childBoards) {
聽聽this.childBoards = childBoards;
聽}
聽public com.lzy.forum.domain.User getCreateBy() {
聽聽return createBy;
聽}
聽public void setCreateBy(com.lzy.forum.domain.User createBy) {
聽聽this.createBy = createBy;
聽}
聽public java.util.Date getCreateTime() {
聽聽return createTime;
聽}
聽public void setCreateTime(java.util.Date createTime) {
聽聽this.createTime = createTime;
聽}
聽public java.lang.Integer getId() {
聽聽return id;
聽}
聽public void setId(java.lang.Integer id) {
聽聽this.id = id;
聽}
聽public java.lang.String getName() {
聽聽return name;
聽}
聽public void setName(java.lang.String name) {
聽聽this.name = name;
聽}
聽public com.lzy.forum.domain.Board getParent() {
聽聽return parent;
聽}
聽public void setParent(com.lzy.forum.domain.Board parent) {
聽聽this.parent = parent;
聽}
聽public java.lang.String getRemark() {
聽聽return remark;
聽}
聽public void setRemark(java.lang.String remark) {
聽聽this.remark = remark;
聽}
}
//Article.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
聽"-//Hibernate/Hibernate Mapping DTD//EN"
聽"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
" >
<hibernate-mapping package="com.lzy.forum.domain">
聽<class
聽聽name="Article"
聽聽table="article"
聽>
聽聽<meta attribute="sync-DAO">false</meta>
聽聽<id
聽聽聽name="Id"
聽聽聽type="integer"
聽聽聽column="id"
聽聽>
聽聽聽<generator class="native"/>
聽聽</id>
聽聽<property
聽聽聽name="ArticleType"
聽聽聽column="article_type"
聽聽聽type="integer"
聽聽聽not-null="true"
聽聽聽length="11"
聽聽/>
聽聽<property
聽聽聽name="Title"
聽聽聽column="title"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽聽length="255"
聽聽/>
聽聽<property
聽聽聽name="Body"
聽聽聽column="body"
聽聽聽type="string"
聽聽聽not-null="false"
聽聽/>
聽聽<property
聽聽聽name="Hits"
聽聽聽column="hits"
聽聽聽type="integer"
聽聽聽not-null="false"
聽聽聽length="10"
聽聽/>
聽聽<property
聽聽聽name="Bytes"
聽聽聽column="bytes"
聽聽聽type="integer"
聽聽聽not-null="false"
聽聽聽length="10"
聽聽/>
聽聽<property
聽聽聽name="CreateTime"
聽聽聽column="create_time"
聽聽聽type="timestamp"
聽聽聽not-null="false"
聽聽聽length="19"
聽聽/>
聽聽<property
聽聽聽name="LastUpdateTime"
聽聽聽column="last_update_time"
聽聽聽type="timestamp"
聽聽聽not-null="false"
聽聽聽length="19"
聽聽/>
聽聽<many-to-one
聽聽聽name="LastUpdateBy"
聽聽聽column="last_update_by"
聽聽聽class="User"
聽聽聽not-null="true"
聽聽>
聽聽</many-to-one>
聽聽<many-to-one
聽聽聽name="CreateBy"
聽聽聽column="create_by"
聽聽聽class="User"
聽聽聽not-null="true"
聽聽>
聽聽</many-to-one>
聽聽<many-to-one
聽聽聽name="Parent"
聽聽聽column="parent_id"
聽聽聽class="Article"
聽聽聽not-null="false"
聽聽>
聽聽</many-to-one>
聽聽<many-to-one
聽聽聽name="Board"
聽聽聽column="board_id"
聽聽聽class="Board"
聽聽聽not-null="true"
聽聽>
聽聽</many-to-one>
聽
聽聽<set name="Articles" inverse="true" lazy="false" order-by="id">
聽聽聽<key column="parent_id"/>銆愯繖閲岀殑parent_id鏄悗鏉ヤ慨鏀圭殑銆?/font>
聽聽聽<one-to-many class="Article"/>
聽聽</set>
聽</class>聽
</hibernate-mapping>
//Article.java
package com.lzy.forum.domain;
import java.util.Date;
聽
public class Article聽 {
聽private static final long serialVersionUID = 1L;
聽// primary key
聽private java.lang.Integer id;
聽// fields
聽private java.lang.Integer articleType;
聽private java.lang.String title;
聽private java.lang.String body;
聽private java.lang.Integer hits;
聽private java.lang.Integer bytes;
聽private java.util.Date lastUpdateTime;
聽private java.util.Date createTime;
聽
聽// many to one
聽private com.lzy.forum.domain.User lastUpdateBy;
聽private com.lzy.forum.domain.User createBy;
聽private com.lzy.forum.domain.Article parent;
聽private com.lzy.forum.domain.Board board;
聽// collections
聽private java.util.Set<com.lzy.forum.domain.Article> articles;
聽public java.util.Set<com.lzy.forum.domain.Article> getArticles() {
聽聽return articles;
聽}
聽public void setArticles(java.util.Set<com.lzy.forum.domain.Article> articles) {
聽聽this.articles = articles;
聽}
聽public java.lang.Integer getArticleType() {
聽聽return articleType;
聽}
聽public void setArticleType(java.lang.Integer articleType) {
聽聽this.articleType = articleType;
聽}
聽public com.lzy.forum.domain.Board getBoard() {
聽聽return board;
聽}
聽public void setBoard(com.lzy.forum.domain.Board board) {
聽聽this.board = board;
聽}
聽public java.lang.String getBody() {
聽聽return body;
聽}
聽public void setBody(java.lang.String body) {
聽聽this.body = body;
聽}
聽public java.lang.Integer getBytes() {
聽聽return bytes;
聽}
聽public void setBytes(java.lang.Integer bytes) {
聽聽this.bytes = bytes;
聽}
聽public com.lzy.forum.domain.User getCreateBy() {
聽聽return createBy;
聽}
聽public void setCreateBy(com.lzy.forum.domain.User createBy) {
聽聽this.createBy = createBy;
聽}
聽public java.lang.Integer getHits() {
聽聽return hits;
聽}
聽public void setHits(java.lang.Integer hits) {
聽聽this.hits = hits;
聽}
聽public java.lang.Integer getId() {
聽聽return id;
聽}
聽public void setId(java.lang.Integer id) {
聽聽this.id = id;
聽}
聽public com.lzy.forum.domain.User getLastUpdateBy() {
聽聽return lastUpdateBy;
聽}
聽public void setLastUpdateBy(com.lzy.forum.domain.User lastUpdateBy) {
聽聽this.lastUpdateBy = lastUpdateBy;
聽}
聽public java.util.Date getLastUpdateTime() {
聽聽return lastUpdateTime;
聽}
聽public void setLastUpdateTime(java.util.Date lastUpdateTime) {
聽聽this.lastUpdateTime = lastUpdateTime;
聽}
聽public com.lzy.forum.domain.Article getParent() {
聽聽return parent;
聽}
聽public void setParent(com.lzy.forum.domain.Article parent) {
聽聽this.parent = parent;
聽}
聽public java.lang.String getTitle() {
聽聽return title;
聽}
聽public void setTitle(java.lang.String title) {
聽聽this.title = title;
聽}
聽public java.util.Date getCreateTime(){
聽聽return createTime;
聽}
聽public void setCreateTime(java.util.Date createTime) {
聽聽// TODO Auto-generated method stub
聽聽this.createTime = createTime;
聽}
聽
}
<script language=javascript>
聽聽聽聽聽聽 document.applet_name.method_name();
</script>
鍙互瀵?/span> applet 榪涜鎺у埗錛屼篃鍙互浠?/span> applet 榪斿洖鍊箋?/span>
Java 璋冪敤 Java Script
闇瑕佸鍏?/span> netscape.javascript.jsobject 榪欎釜綾?/span> 錛坖re/lib/plugin.jar)
姣斿錛?/span>
JSObject window=JSObject.getWindow(this);
JSObject window=(JSObject)window.getMember("document");
鏉ヨ幏鍙栧褰撳墠紿楀彛鐨勬帶鍒舵潈銆?/span>
褰撶偣鍑婚摼鎺?/span>
MakeCall
鏃訛紝浼氳Е鍙?/span>
Java Script
鍑芥暟
function makeCall(s){
聽 window.document.ctiApplet.testJavascript(s);
聽 alert("call maked!");
聽}
鍦ㄨ繖涓?/span> Java Script 鍑芥暟涓紝 ctiApplet 鏄竴涓?/span> Applet 錛岃繖鏍峰畬鎴愪簡 java script 璋冪敤
Applet 銆?/span>
浠ヤ笅鏄?/span> ctiApplet 涓殑 testJavascript 鍑芥暟鍜岃繍琛岀粨鏋滐紝鍏朵腑鍙堣皟鐢ㄤ簡 Java Script 銆?br /> public void testJavascript(String msg){
聽聽聽聽聽聽
聽聽聽聽聽聽 try {
聽聽聽聽聽聽聽聽聽聽聽聽聽 // create JSObject
聽聽聽聽聽聽 聽聽聽 聽聽聽聽聽聽 JSObject.getWindow (this).eval ("javascript:alert('Java Script in Applet!')") ;
聽聽聽聽聽聽 }
聽聽聽聽聽聽 catch (Exception ex) {
聽聽聽聽聽聽聽聽聽聽聽聽聽 // Error on create JSObject
聽聽聽聽聽聽 聽聽聽 聽聽聽聽聽聽 showStatus( "Error call javascript err=" + ex );
聽聽聽聽聽聽 }
聽聽聽聽聽聽 System.out.println(msg);
}
聽
鏍規嵁瀹夎璇存槑錛孞Boss 4.0.3SP1涓殑EJB3瀹炵幇涓嶆槸鏈鏂扮殑錛屽簲鐢ㄤ笅杞界殑jboss-EJB-3.0_RC5-PFD涓殑涓浜涘寘鍘繪浛鎹€?/p>
灝唈boss-EJB-3.0_RC5-PFD涓璴ib鐩綍涓嬬殑
聽(1)ejb3.deployer/
聽(2)ejb3-clustered-sfsbcache-service.xml
聽(3)ejb3-interceptors-aop.xml
聽(4)jboss-aop-jdk50.deployer/
鎷瘋礉鍒癹boss-4.0.3SP1/server/all/deploy鐩綍涓?/p>
灝唈boss-EJB-3.0_RC5-PFD涓璴ib鐩綍涓嬬殑
聽(5)jboss-remoting.jar
聽(6)jboss-serialization.jar
聽(7)jboss-j2ee.jar
鎷瘋礉鍒癹boss-4.0.3SP1/server/all/lib鐩綍涓?/p>
鍒犻櫎jboss-4.0.3SP1/server/all/lib 涓嬬殑
聽(1)ejb3-persistence.jar
聽(2)hibernate3.jar
聽(3)hibernate-annotations.jar
聽(4)hibernate-entitymanager.jar
鍜宩boss-4.0.3SP1/server/all/deploy涓嬬殑
聽(5)jboss-aop.deployer/
鍒版EJB3 鐨勮繍琛岀幆澧冨噯澶囧ソ浜嗭紝浣犲彲浠ョ敤jboss-4.0.3SP1\bin\run -c all 嫻嬭瘯JBoss鏄惁鑳芥垚鍔熷惎鍔ㄣ?/p>
鎴戠敤鐨処DE鏄疎clipse 3.2錛孞DK 1.5 錛屼負浜嗘柟渚垮湪Eclipse涓柊寤轟竴涓猆ser Library錛欵JB3_JBoss錛?br />灝唈boss-EJB-3.0_RC5-PFD\lib涓嬬殑
聽jboss-j2ee.jar
鍜宩boss-EJB-3.0_RC5-PFD\lib\ejb3.deployer涓嬬殑
聽ejb3-persistence.jar
聽jboss-ejb3x.jar
聽jboss-ejb3.jar
聽jboss-annotations-ejb3.jar
聽hibernate3.jar
聽hibernate-annotations.jar
聽hibernate-entitymanager.jar
娣誨姞鍒癊JB_JBoss涓紝鐜板湪鐜鍩烘湰涓婃惌濂戒簡銆?/p>
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author lzy
*
*/
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
鍦╳eb.xml 鏂囦歡涓綔濡備笅璁劇疆錛氾紙鎴戜嬌鐢ㄧ殑鏄疭truts妗嗘灦錛?BR><filter>
<filter-name>Encoding</filter-name>
<filter-class>com.lzy.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<servlet-name>action</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
鏈鍚庯紝榪炴帴鏁版嵁搴撴椂錛屼嬌鐢ㄤ笅闈㈢殑url錛?BR>jdbc:mysql://localhost:3306/鏁版嵁搴撳悕?useUnicode=true&characterEncoding=GBK
/**
* @author lzy
*
*/
public class HelloBean{
private String hello;
public void sayHello(){
System.out.println(this.getHello());
}
/**
* @return Returns the hello.
*/
public String getHello() {
return hello;
}
/**
* @param hello The hello to set.
*/
public void setHello(String hello) {
this.hello = hello;
}
}
4.鏂板緩鏂囦歡bean.xml錛屽皢鍦ㄨ繖涓猉ML鏂囦歡涓負涓涓狧elloBean鐨勫疄渚嬬殑hello灞炴ц祴鍊箋?BR>//bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "<beans>
<description>Spring Quick Start</description>
<bean id="helloBean" class="com.lzy.HelloBean">
<property name="hello">
<value>hello world</value>
</property>
</bean>
</beans>
5.鏂板緩嫻嬭瘯綾伙細TestSpring
//TestSpring.java
/**
*
*/
package com.lzy;
import java.util.Locale;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
/**
* @author lzy
*
*/
public class TestSpring {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx=new FileSystemXmlApplicationContext("bean.xml");
HelloBean hello=(HelloBean)ctx.getBean("helloBean");
hello.sayHello();
}
}
6.榪愯嫻嬭瘯綾伙細
濡傛灉娌℃湁鍑洪敊錛岃緭鍑轟腑灝嗕細鐪嬪埌鈥渉ello world鈥濄?BR>
Plugin Search:
http://eclipse-plugins.2y.net/eclipse/search.jsp
Hibernate synchronizer鎻掍歡鐨勫畨瑁呭拰閰嶇疆鏈夐棶棰樿鐩存帴Google銆?
鍦ㄥ伐紼嬩腑鍏跺疄鍙敤鍒頒簡Hibernate synchronizer鎻掍歡鐨勪竴閮ㄥ垎鍔熻兘錛孒ibernate渚濊禆鐨勭浉鍏砵ar鍖呮渶濂借繕鏄墜鍔ㄦ坊鍔狅紝鍥犱負鏈寮濮嬬敤Hibernate synchronizer娣誨姞鏃舵繪槸鍙戠敓閿欒銆?BR> 灝嗕笅杞界殑Mysql driver鍜孒ibernate鍖呰В鍘嬬緝錛屾垜浠渶瑕佺殑鍙槸閲岄潰鐩稿叧鐨刯ar錛屽湪Eclipse涓柊寤篗ysql_Driver鍜孒ibernate涓や釜user library,灝唌ysql-connector-java-3.0.15-ga-bin.jar鍔犲叆Mysql_Driver涓紝灝唄ibernate3.jar錛?BR>錛宭og4j-1.2.11.jar錛宎ntlr-2.7.5H3.jar錛宎sm.jar錛宎sm-attrs.jar錛宑glib-2.1.2.jar錛宑ommons-collections-2.1.1.jar錛宑ommons-logging-1.0.4.jar錛宒om4j-1.6.1.jar錛宔hcache-1.1.jar錛宩ta.jar鍔犲叆鍒癏ibernate涓?/P>
2.寮濮?
鍦∕ysql涓柊寤簍est鏁版嵁搴擄紙Mysql鍏跺疄鏈変釜絀虹殑test鏁版嵁搴擄級錛岀劧鍚庢柊寤轟笅闈㈢殑Table
create table user (
id int(10) not null auto_increment primary key,
name varchar(20) not null,
password varchar(20) not null,
email varchar(50),
address varchar(100)
)type=innodb;
鏂板緩Java Project錛屽皢Mysql_Driver,Hibernate涓や釜user library娣誨姞鍒拌宸ョ▼鐨刯ava build path涓?/P>
鏂板緩涓庢暟鎹〃瀵瑰簲鐨凱OJO綾伙細User鍜孋ontact
/**
*
*
*/
package com.user;
/**
* @author lzy
*
*/
public class User{
private Integer id;
private String name;
private String password;
private Contact contact;
/**
* @return Returns the id.
*/
public Integer getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return Returns the contact.
*/
public Contact getContact() {
return contact;
}
/**
* @param contact The contact to set.
*/
public void setContact(Contact contact) {
this.contact = contact;
}
}
/**
*
*/
package com.user;
/**
* @author lzy
*
*/
public class Contact {
private String email;
private String address;
/**
* @return Returns the address.
*/
public String getAddress() {
return address;
}
/**
* @param address The address to set.
*/
public void setAddress(String address) {
this.address = address;
}
/**
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
}
涔嬪悗鍙互鐢╯ynchronizer鎻掍歡鐢熸垚Hibernate閰嶇疆鏂囦歡鍜屾槧灝勬枃浠訛紙鐩稿叧榪囩▼鍙互鍙傝?A >http://www.ideagrace.com/html/doc/2005/08/01/00315.html)錛屼笉榪囨槧灝勬枃浠跺繀欏葷◢浣滀慨鏀廣?/P>
hibernate.cfg.xml <hibernate-configuration> <!-- local connection properties --> <!-- dialect for MySQL --> <property name="hibernate.show_sql">True</property> User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"
<session-factory >
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username"></property>
<property name="hibernate.connection.password"></property>
<!-- property name="hibernate.connection.pool_size"></property -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<mapping resource="User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
1.寮鍙戠幆澧?BR> 寮鍙戜箣鍓嶉鍏堝簲璇ュ噯澶囧ソ寮鍙戠幆澧冿紝Eclipse 3.X鏄竴涓笉閿欑殑Java IDE錛屾湁寰堝鎻掍歡鍙互渚涙垜浠夋嫨錛屾湇鍔″櫒鐢═omcat灝辮浜嗐傚畨瑁呭ソEclipse鍜孴omcat涔嬪悗錛岃繕闇瑕佷袱涓彃浠訛細tomcat 鎻掍歡鍜孲truts鎻掍歡錛屼互涓嬫槸鐩稿叧涓嬭澆鍦板潃錛?BR> Eclipse SDK錛?BR> http://www.eclipse.org/downloads/index.php
Struts:
https://sourceforge.jp/projects/amateras/files/ StrutsIDE+htmlEditor錛宧tmlEditer涔熸槸蹇呰鐨?BR> Tomcat :
http://www.sysdeo.com/eclipse/tomcatplugin
Plugin Search:
http://eclipse-plugins.2y.net/eclipse/search.jsp
鎻掍歡鐨勫畨瑁呭拰閰嶇疆鏈夐棶棰樿鐩存帴Google銆?BR>2.寮濮?BR> 鍏ラ棬鍢涳紝鎴戜滑灝辨壘涓涓渶綆鍗曠殑Login灝卞彲浠ヤ簡
鏂板緩Tomcat project錛屽涓嬪浘
鍔犲叆struts鏀寔
鏂板緩涓涓狥ormBean:LoginForm
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* @author lzy
*
*/
public class LoginForm extends ActionForm {
private String name=null;
private String password=null;
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public void reset(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
name=null;
password=null;
}
/* (non-Javadoc)
* @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
*/
@Override
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
// TODO Auto-generated method stub
ActionErrors errors=new ActionErrors();
if(name==null||name.length()<1){
errors.add("username",new ActionMessage("errors.login.name.empty"));
}
if(password==null||password.length()<1){
errors.add("password",new ActionMessage("errors.login.password.empty"));
}
return errors;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the password.
*/
public String getPassword() {
return password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
}
鏂板緩涓涓狝ction:LoginAction
/**
*
*/
package com.test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.*;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
/**
* @author lzy
*
*/
public class LoginAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
LoginForm loginForm=(LoginForm)actionForm;
String name=loginForm.getName();
String password=loginForm.getPassword();
if(name.equals("name")&&password.equals("password")){
HttpSession session=request.getSession(true);
session.setAttribute("LOGGED_USER",name);
return mapping.findForward("success");
}
else{
ActionMessages errors=new ActionMessages();
errors.add("login.failure",new ActionMessage("errors.login.failure"));
this.saveErrors(request,errors);
return mapping.findForward("failure");
}
}
}
鏂板緩涓や釜JSP欏甸潰錛宭ogin.jsp,index.jsp
login.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-tiles" prefix="tiles" %>
<html:html>
<head>
<html:base />
</head>
<body>
<center>
<html:errors />
<html:form action="/login.do" method="POST">
<table border="1">
<tr>
<td colspan="2"><bean:message key="login.jsp.form.title"/></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.name"/></td>
<td><html:text property="name" size="20"></html:text></td>
</tr>
<tr>
<td align="right"><bean:message key="login.jsp.password"/></td>
<td><html:password property="password" size="20"></html:password></td>
</tr>
<tr>
<td colspan="2">
<html:submit><bean:message key="form.submit"/></html:submit>
<html:reset><bean:message key="form.reset"/></html:reset>
</td>
</tr>
</table>
</html:form>
</center>
</body>
</html:html>
index.jsp
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-nested" prefix="nested" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK"/>
<title></title>
</head>
<body>
hello
<logic:present name="LOGGED_USER">,<bean:write name="LOGGED_USER"/>
</logic:present>
</body>
</html:html>
緙栬緫WEB錛岻NF/lib涓嬬殑struts錛峜onfig.xml
struts-config.xml
<?xml version="1.0"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "<struts-config>
<data-sources>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="com.lzy.LoginForm"/>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/login" name="loginForm" type="com.lzy.LoginAction" scope="session" validate="true" input="/login.jsp">
<forward name="failure" path="/login.jsp"/>
<forward name="success" path="/index.jsp"/>
</action>
</action-mappings>
<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
<message-resources parameter="MessageResources"/>
</struts-config>
鏈鍚庢槸璧勬簮鏂囦歡
# -- standard errors --
errors.header=<UL><font color="red"><h2>Errors:</h2>
errors.prefix=<LI>
errors.suffix=</LI>
errors.footer=</font></UL><hr>
form.submit=Submit
form.reset=Reset
# -- login.jsp --
login.jsp.title=Login
login.jsp.form.title=Login
login.jsp.name=Name:
login.jsp.password=Password:
# -- login errors --
errors.login.name.empty=The name can not be empty!
errors.login.password.empty=The password can not be empty!
errors.login.failure=Login failed!<br> Please check your username and password!
3.嫻嬭瘯
鍦╰est宸ョ▼涓夋嫨tomcat project錛?gt;Update context definition
鐒跺悗榪愯Tomcat
http://127.0.0.1:8080/test/login.jsp
//鐣欒█bean
package userinfobean;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class memoBean {
private String id;//1.鐢ㄦ埛鍚?BR> private String email;//2.瀵嗙爜
private String content;//3.鐣欒█
public memoBean() {
}
//1.鐢ㄦ埛鍚?BR> public void setId(String s){
this.id=s;
}
//2.瀵嗙爜
public void setEmail(String s){
this.email=s;
}
//3.鐣欒█
public void setContent(String s){
this.content=s;
}
//1.鐢ㄦ埛鍚?BR> public String getId( ){
return this.id;
}
//2.瀵嗙爜
public String getEmail(){
return this.email;
}
//3.鐣欒█
public String getContent(){
return this.content;
}
}
//// 鐢ㄤ簬鏁版嵁搴撴搷浣滅殑bean
package userinfobean;
import userinfobean.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class databaseBean{
private String dbName;
private String dbUser;
private String dbPass;
private Vector memoVector;
Connection connection;
public databaseBean(){
dbName=new String("脳脳脳脳脳脳脳");
dbUser=new String("脳脳脳脳脳脳");
dbPass=new String("脳脳脳脳脳脳脳");
String connectionUrl="jdbc:mysql://localhost/"+dbName;
try{
Class.forName("org.gjt.mm.mysql.Driver");
connection=DriverManager.getConnection(connectionUrl,dbUser,dbPass);
}
catch(Exception e){
System.out.println(e.toString());
}
}
public void dbQueryMemos(){
try{
memoVector=new Vector();
Statement stmt=connection.createStatement();
ResultSet rs=stmt.executeQuery("select * from memo;");
while(rs.next()){
memoBean temp=new memoBean();
temp.setId(rs.getString(2));
temp.setEmail(rs.getString(3));
temp.setContent(rs.getString(4));
memoVector.add(temp);
}
}
catch(SQLException e){
System.out.println(e.toString());
}
}
public void setDbName(String s){
this.dbName=s;
}
public void setDbUser(String s){
this.dbUser=s;
}
public void setDbPass(String s){
this.dbPass=s;
}
public String getDbName(){
return this.dbName;
}
public String getDbUser(){
return this.dbUser;
}
public String getDbPass(){
return this.dbPass;
}
public Vector getMemoVector(){
return this.memoVector;
}
}
///浠ヤ笅鏄綉欏墊簮鐮?BR>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ page language="java" import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="userinfobean.*" %>
<jsp:useBean id="memoInfo" scope="page" class="userinfobean.memoBean"/>
<jsp:useBean id="memoQuery" scope="page" class="userinfobean.databaseBean"/>
<%!
int totalPages;
int countPerPage=3;
int totalCount;
int currentPage=0;
int currentStart;
%>
<%
memoQuery.dbQueryMemos();
Vector vt=memoQuery.getMemoVector();
totalCount=vt.size();
totalPages=totalCount/countPerPage;
if(totalCount%countPerPage>0)totalPages=totalPages+1;
if(request.getParameter("page")==null)
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("page"));
if(currentPage>totalPages)currentPage=totalPages;
if(currentPage<1)currentPage=1;
}
currentStart=totalCount-1-countPerPage * (currentPage-1);
%>
<html>
<head>
<title>NetGreen</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="main.css">
</head>
<script language="JavaScript" >
function page_check()
{
if(document.pageForm.page.value=="")
{alert("璇瘋緭鍏ラ〉鏁?);
document.pageForm.page.focus();
return false;
}
return true;
}</script>
<body bgcolor="ffffff" >
<center>
<table width="800" border="0" height="622">
<tr>
<td height="71" align="center" valign="middle"> <table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC">
<tr>
<td width="19%" height="60" align="left" valign="bottom" bgcolor="#FFFFFF"><b><font size="4" color="#00cc66" ><img src="image/logo.gif" width="156" height="60"></font></b></td>
<td width="57%" align="left" valign="bottom" bgcolor="#FFFFFF"><img src="image/banner.gif" width="100%" height="60"></td>
<td width="24%" align="left" valign="bottom"> <table width="102%" height="60" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC" bgcolor="#FFFFFF">
<tr>
<td width="50%" height="27" align="center">涓?浼?lt;/td>
<td width="50%" align="center">鏀惰棌鏈〉</td>
</tr>
<tr>
<td height="21" align="center" bgcolor="#FFFFFF" >閫 鍑?</td>
<td align="center"><a href="memoForm.htm" target="_self">鎴戣鐣欒█</a></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="31"><table width="100%" border="1" bordercolor="#0099CC">
<tr bgcolor="#00CC66">
<td width="12%" height="23" align="center" bgcolor="#CCFFFF"><a href="index.htm" >棣栭〉</a></td>
<td width="12%" align="center" bgcolor="#CCFFFF"><a href="loginForm.htm" >鐧婚檰</a></td>
<td width="12%" align="center" bgcolor="#CCFFFF"> </td>
<td width="12%" align="center" bgcolor="#CCFFFF"> </td>
<td width="12%" align="center" bgcolor="#CCFFFF" > </td>
<td width="12%" align="center" bgcolor="#CCFFFF"> </td>
<td width="12%" align="center" bgcolor="#CCFFFF"><a href="memoDisplay.jsp" >鐣欒█鏉?lt;/a></td>
<td width="12%" align="center" bgcolor="#CCFFFF"><a href="registForm.htm">娉ㄥ唽</a></td>
</tr>
</table></td>
</tr>
<tr>
<td height="435" align="center" valign="top" nowrap>
<table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="#0099CC">
<tr>
<td width="18%" height="431" align="center" valign="middle" bgcolor="#CCFFFF">
</td>
<td width="83%" align="center" valign="top" bgcolor="#FFFFFF"><table width="100%" border="1"><form name="pageForm" method="post" onSubmit="return page_check();"action="memoDisplay.jsp">
<tr>
<td width="36%">鍏辨湁鐣欒█<%out.print(totalPages);%> 欏碉紝鐜板湪鏄<%out.print(currentPage);%> 欏?lt;/td>
<td width="64%">
<a href="memoDisplay.jsp?page=<%=currentPage-1%>" >涓婁竴欏?</a>
<a href="memoDisplay.jsp?page=<%=currentPage+1%>" >涓嬩竴欏?nbsp; </a>
<a href="memoDisplay.jsp?page=1" >棣栭〉 </a>
<a href="memoDisplay.jsp?page=<%=totalPages%>" >灝鵑〉 </a>
<input type="submit" name="Submit" value="G0">
<input name="page" type="text" size="10">
欏?lt;/td>
</tr>
</form></table>
<%
for(int i=0;i<countPerPage&¤tStart-i>=0;i++)
{
memoInfo.setId(((memoBean)vt.get(currentStart-i)).getId());
memoInfo.setEmail(((memoBean)vt.get(currentStart-i)).getEmail());
memoInfo.setContent(((memoBean)vt.get(currentStart-i)).getContent());
out.print("<table width='100%' border='1' ><tr><td width='19%'>");
out.print(memoInfo.getId());
out.print("</td><td width='81%'>");
out.print(memoInfo.getEmail());
out.print("</td></tr><tr><td height='44' colspan='2'>");
out.print(memoInfo.getContent());
out.print("</td></tr></table> ");
out.print("<br>");
}
%>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="73" align="center" valign="top" nowrap>
<table width="100%" border="0">
<tr>
<td height="21" align="center" bgcolor="#FFFFFF"><hr width="85%" noshade color="#00CC66"></td>
</tr>
<tr>
<td height="12" align="center">Copyright @ HomeLee. All rights reserved.
</td>
</tr>
<tr>
<td height="12" align="center" bgcolor="#FFFFFF"> </td>
</tr>
</table></td>
</tr>
</table>
</center></body>
</html>