隨筆 - 11  文章 - 11  trackbacks - 0

          留言簿(2)

          隨筆分類(13)

          隨筆檔案(11)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          在Struts1.1-1.2中如果不想沒有登錄的用戶訪問一些action,可以擴(kuò)展RequestProcessor,并重載processProprocess方法,在其中進(jìn)行驗(yàn)證

          public class CustomRequestProcessor
                  extends RequestProcessor {
              protected boolean processPreprocess (
                      HttpServletRequest request,
                      HttpServletResponse response) {
                  HttpSession session = request.getSession(false);
                  //If user is trying to access login page
                  // then don't check
                  if( request.getServletPath().equals("/loginInput.do")
                      || request.getServletPath().equals("/login.do") )
                      return true;
                  //Check if userName attribute is there is session.
                  //If so, it means user has allready logged in
                  if( session != null &&
                  session.getAttribute("userName") != null)
                      return true;
                  else{
                      try{
                          //If no redirect user to login Page
                          request.getRequestDispatcher 
                              ("/Login.jsp").forward(request,response);
                      }catch(Exception ex){
                      }
                  }
                  return false;
              }
          }
          

          相應(yīng)的,在struts-config.xml中加入controller元素

          <controller>
           <set-property  property="processorClass"
           value="com.sample.util.CustomRequestProcessor"/>
          </controller>
          

          但剛才看到在Struts1.3中,已經(jīng)不再使用RequestProcessor,而是用ComposableRequestProcessor 來實(shí)現(xiàn)自定義的request響應(yīng)。






          posted @ 2007-04-09 18:52 hijeff 閱讀(446) | 評(píng)論 (0)編輯 收藏

          三個(gè)@Entity類Tc, Tracer, Track是三向關(guān)聯(lián)的的表, @Embeddable類TcTracer則存儲(chǔ)著三個(gè)表的ForeignKey

          @Entity
          @Table(name= "tc", catalog="first_hiber")
          public class Tc {
                @org.hibernate.annotations.CollectionOfElements
                @JoinTable(name="tc_tracer", joinColumns=@JoinColumn(name="tc_id"))
                private Set<TcTracer> tcTracers = new HashSet<TcTracer>();
          
                @Id @GeneratedValue
                @Column(name="tc_id", nullable= false)
                private Long id;
          }
          
            @Embeddable
            @Table(name="tc_tracer")
            public class TcTracer {
                @org.hibernate.annotations.Parent
                private Tc tc;
            
          @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="tracer_id", nullable=false, updatable=false) private Tracer tracer; @ManyToOne(cascade=CascadeType.ALL) @JoinColumn(name="track_id", nullable=false, updatable=false) private Track track; }

          如上,將TcTracer作為Tc的子元素,從而實(shí)現(xiàn)Tc的三相關(guān)聯(lián)。

          用HQL可以通過特定的Tc,Tracer得到對(duì)應(yīng)的所有Track:

            select tctracers.track
            from Tc tc
                join tc.tcTracers tctracers
            where tc.name="tc name"
                and tctracers.tracer.name="tracer name"
          
           List<Track> tracks = session.createQuery(HQL).list();
          
          posted @ 2007-04-02 17:45 hijeff 閱讀(386) | 評(píng)論 (0)編輯 收藏

          今天調(diào)試程序的時(shí)候,發(fā)現(xiàn)插入數(shù)據(jù)時(shí)老是拋出異常:"a different object with the same identifier value was already associated with the session"

          起初以為是override hashCode()的方法不對(duì)。后來google查了查多數(shù)問題都出現(xiàn)在detached對(duì)象之后,再鏈接session由于對(duì)象的改變而出現(xiàn)的異常。

          最后發(fā)現(xiàn)原來問題很簡(jiǎn)單....是在用annotation重寫mapping的時(shí)候,忘記給@Id加上@GeneratedValue,導(dǎo)致第一個(gè)row insert后,再次insert時(shí)沒有生成新的id!

          看來annotation雖然方便了mapping,但由于和代碼集中在一塊了,很容易出現(xiàn)疏忽大意的錯(cuò)誤,而且一般都不會(huì)注意到-.-

          想起了python的SQLObject, SQLAlchemy,雖然功能沒有Hibernate豐富,但是簡(jiǎn)單實(shí)用!

          posted @ 2007-03-29 20:28 hijeff 閱讀(414) | 評(píng)論 (0)編輯 收藏
          主站蜘蛛池模板: 收藏| 民权县| 远安县| 汾西县| 临城县| 鹤庆县| 绥阳县| 格尔木市| 蒙山县| 旬邑县| 阿拉善右旗| 沿河| 盘山县| 田东县| 平顶山市| 中江县| 雅安市| 广汉市| 洛阳市| 大英县| 柘荣县| 寿宁县| 尼勒克县| 新郑市| 军事| 会东县| 凌源市| 阳朔县| 芷江| 廉江市| 科尔| 蕲春县| 焉耆| 康乐县| 东方市| 特克斯县| 山东省| 临澧县| 防城港市| 历史| 五台县|