-
完成顯示登陸成功顯示用戶名:
1、返回登陸成功頁面前,在session中保存登陸成功的用過戶名。
Action中l(wèi)ogin方法:session.getAttribute("loginUserName",user.getUsername());
前端頁面獲取方式:
JSPsession獲取方式:<%=session.getAttribute("loginUserName"%>
EL表達式獲取方式:${sessionScope.loginUserName}
注銷功能:編寫注銷方法(判斷session中是否保存了用戶登錄成功的用戶名,如果有則清空,然后返回一個字符串注銷標識,緊接著配置struts.xml)
session的removeAttribute(""):移除指定key的value。
查看全部 -
設計所有Action父類:
1、繼承ActionSupport:內置很多struts攔截器,方便以后使用。
2、獲得常用的內置對采用偶合IOC方式注入屬性,以至于與前端進行聯系,
實現ServletRequestAware、ServletResponseAware、ServletContext、并通過方法為request、response、application、session(通過request.getSession())賦值。
查看全部 -
struts2的三種接收表單數據的方式
查看全部 -
對象關系映射文件
查看全部 -
最新API方法:
Configuration configuration = new Configuration().configure(); ServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build(); SessionFactory sessionfactory = configuration.buildSessionFactory(registry); Session session = sessionfactory.getCurrentSession(); MetadataImplementor metadata = (MetadataImplementor) new MetadataSources(registry).buildMetadata(); SchemaExport export = new SchemaExport(registry,metadata); export.create(true,true);
查看全部 -
生成表結構:
//?創(chuàng)建配置對象
Configuration?config?=?new?Configuration().configue();
//?創(chuàng)建服務注冊對象
ServiceReqistry?serviceReqistry?=?new?ServiceRegistryBuilder().applySetting(config);
//?創(chuàng)建sessionFactory
SessionFactory?sessionFactory?=?config.buildeSessionFactory(serviceReqistry);
//?創(chuàng)建session對象
Session?session?=?sessionFactory.getCurrentSession();
//?創(chuàng)建schemaExport對象
SchemaExport?export?=?new?SchemaExport(config);
?export.create(true,?true);?//?第一個true表示生成表結構,第二個表示輸出SQL語句
查看全部 -
靜態(tài)方法不能獲取非靜態(tài)屬性
查看全部 -
private?static?SessionFactory?sessionFactory;??//?會話工廠屬性 //?構造方法私有化,保證單例模式 private?MyHibernateSessionFactory(){} //?公有的靜態(tài)方法,獲得會話工廠對象 public?static?SessionFactory?getSessionFactory(){ ????if(sessionFactory==null){ ????????Configuration?config?=?new?Configuration().configure(); ????????ServiceReqistry?serviceReqistry?=?new?ServiceReqistryBuilder().applySettings(config.getProperties()).buildServiceReqistry(); ????????sessionFactory?=?config.buildSessionFactory(serviceReqistry); ????????return?sessionFactory; ????}?else{ ????????return?sessionFactory; ????} }
查看全部 -
生成表結構: //?創(chuàng)建配置對象 Configuration?config?=?new?Configuration().configue(); //?創(chuàng)建服務注冊對象 ServiceReqistry?serviceReqistry?=?new?ServiceRegistryBuilder().applySetting(config); //?創(chuàng)建sessionFactory SessionFactory?sessionFactory?=?config.buildeSessionFactory(serviceReqistry); //?創(chuàng)建session對象 Session?session?=?sessionFactory.getCurrentSession(); //?創(chuàng)建schemaExport對象 SchemaExport?export?=?new?SchemaExport(config); export.create(true,?true);?//?第一個true表示生成表結構,第二個表示輸出SQL語句
查看全部 -
實體類的映射文件
查看全部 -
Struts2與Hibernate整合: ????1.?創(chuàng)建struts2和hibernate用戶類庫 ????2.?導入struts2與hibernate的jar包 ????3.?配置web.xml文件(加入struts2的過濾器) ????<filter> ????????<filter-name>struts2</filter-name> ????????<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>?//?struts2過濾器 ????</filter> ????<filter-mapping> ????????<filter-name>struts2</filter-name> ????????<url-pattern>/*</url-pattern>??//?過濾所有請求 ????</filter-mapping> ????4.?創(chuàng)建struts.xml ?????WEB-INF/classes/struts.xml?-->?src/struts.xml ?????<package?name="default"?namespace="/"?extends="struts-default"> ???? ?????</package> ????5.?配置hibernate.cfg.xml(hibernate的主配置文檔) ????src/hibernate.cfg.xml ??<hibernate-configuration> ???<session-factory> ????<property?name="connection.username">root</property> ????<property?name="connection.password"></property> ????<property?name="connection.driver_class">com.mysql.jbdc.Driver</property> ????<property ????????name="connection.url">jdbc:mysql:///test?useUnicode=true&characterEncoding=UTF-8</property> ????<property?name="dialect">org.hibernate.dialet.MySQLDialect</property> ????<property?name="show_sql">true</property> ????<property?name="format_sql">true</property> ????<property?name="hbm2dd1.auto">update</property> ????<property?name="hibernate.current_session_context_class">thread</property>//?使用getCurrentSession方式打開會話 ???</session-factory> ??</hibernate-configuration>
查看全部 -
修改學生資料
注意:學號不能修改
查看全部 -
說明:每次遍歷。將session中的list的值取出一個放到對象stu中,然后從stu中取出Student類的信息。
查看全部 -
加入驗證方法后, 輸入不合法的會報錯,
No result defined for action action.UsersAction and result input - action - file:/D:/InstallSoft/Apache%20Software%20Foundation/Tomcat%207.0/webapps/imooc_sh/WEB-INF/classes/struts.xml:11:59
解決方法:在struts2配置文件中配置下
<result name="input">/users/Users_login.jsp</result>
原理:input是struts默認的邏輯視圖名稱,表示當前頁面輸入參數驗證錯誤時,會自動返回要跳轉的頁面。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
查看全部 -
配置hibernate.cfg.xml查看全部
舉報