<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開發
    隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
    數據加載中……

    一道Struts面試題

    題目是這樣的
    有兩張表
    一張為新聞類別表
    有2個字段:

    nid(pk)         sort


    有一張新聞內容表

    有三個字段

    cid(pk)       nid(fk)     title     content

    要求通過下拉列表框的方法選擇新聞類別然后顯示該類別的新聞標題(在當前頁中顯示)
     我是用Struts2+Hibernate3.2+JPA實現的.
    數據庫腳本:
    create database if not exists news;
    drop table if exists newssort;
    create table newssort 
    (
      nid 
    int primary key AUTO_INCREMENT,
      sort 
    varchar(50)
    );

    drop table if exists news;
    create table news
    (
      cid 
    int primary key AUTO_INCREMENT,
      title 
    varchar(50not null,
      content 
    varchar(500not null,
      nid  
    int null
    );

    insert into newssort values(null,'娛樂');
    insert into newssort values(null,'時事');

    insert into news values(null,'好事','好事連連哈哈',1);
    insert into news values(null,'壞事','壞事不斷',1);
    insert into news values(null,'愛情是什么','愛情是什么啊,還沒知道呢',2);
    insert into  news values(null,'什么啊','測試內容',2);

    select * from news;
    select * from newssort;
    兩個VO類:
    News.java:
    package com.vo;

    import java.io.Serializable;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @SuppressWarnings(
    "serial")
    @Entity
    @Table(name
    ="news")
    public class News implements Serializable
    {
        
    private Integer cid;
        
    private String title;
        
    private String content;
        @Id
        @GeneratedValue(strategy
    =GenerationType.AUTO)
        
    public Integer getCid()
        
    {
            
    return cid;
        }


        
    public void setCid(Integer cid)
        
    {
            
    this.cid = cid;
        }


        
    public String getTitle()
        
    {
            
    return title;
        }


        
    public void setTitle(String title)
        
    {
            
    this.title = title;
        }


        
    public String getContent()
        
    {
            
    return content;
        }


        
    public void setContent(String content)
        
    {
            
    this.content = content;
        }

    }

    Newssort.java:
    package com.vo;

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;

    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    import org.hibernate.annotations.LazyCollection;
    import org.hibernate.annotations.LazyCollectionOption;

    @SuppressWarnings(
    "serial")
    @Entity
    @Table(name 
    = "newssort")
    public class Newssort implements Serializable
    {
        
    private Integer nid;
        
    private String sort;
        
    private List<News> news = new ArrayList<News>();
        @OneToMany
        @JoinColumn(name
    ="nid")
        @LazyCollection(LazyCollectionOption.FALSE)
        
    public List<News> getNews()
        
    {
            
    return news;
        }


        
    public void setNews(List<News> news)
        
    {
            
    this.news = news;
        }


        @Id
        @GeneratedValue(strategy 
    = GenerationType.AUTO)
        
    public Integer getNid()
        
    {
            
    return nid;
        }


        
    public void setNid(Integer nid)
        
    {
            
    this.nid = nid;
        }

        
        
    public String getSort()
        
    {
            
    return sort;
        }


        
    public void setSort(String sort)
        
    {
            
    this.sort = sort;
        }

    }


    寫個測試類先測試一個持久層操作:
    package com.test;

    import java.util.Iterator;
    import org.hibernate.Session;
    import org.hibernate.cfg.AnnotationConfiguration;
    import org.junit.After;
    import org.junit.Before;

    import com.vo.News;
    import com.vo.Newssort;
    public class Test
    {
        
    private Session session ;
        @Before
        
    public void setUp()
        
    {
            session 
    = new AnnotationConfiguration().configure().buildSessionFactory().openSession();
        }

        @After
        
    public void tearDown()
        
    {
            session.close();
        }

        
        @SuppressWarnings(
    "unchecked")
        @org.junit.Test
        
    public void testFind()
        
    {
            @SuppressWarnings(
    "unused")
            
    //List<Newssort> newssort = session.createCriteria(Newssort.class).list();
            Newssort newssort = (Newssort) session.load(Newssort.class2);
            
    for(Iterator<News> i = newssort.getNews().iterator();i .hasNext();)
            
    {
                String title 
    = i.next().getTitle();
                System.out.println(title);
            }

        }

    }

    好了寫Action
    NewsAction:
    package com.web.action;

    import java.util.List;
    import java.util.Map;

    import org.hibernate.Session;
    import org.hibernate.cfg.AnnotationConfiguration;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    import com.vo.News;
    import com.vo.Newssort;

    @SuppressWarnings( 
    "serial""unchecked" })
    public class NewsAction extends ActionSupport
    {
        
    private Session session;
        
    private Integer sortid;

        
    public Integer getSortid()
        
    {
            
    return sortid;
        }


        
    public void setSortid(Integer sortid)
        
    {
            
    this.sortid = sortid;
        }


        
    public void init()
        
    {
            session 
    = new AnnotationConfiguration().configure()
                    .buildSessionFactory().openSession();
        }


        
    public String findNewssort()
        
    {
            
    this.init();
            List
    <Newssort> sorts = session.createCriteria(Newssort.class).list();
            Map request 
    = (Map) ActionContext.getContext().get("request");
            request.put(
    "sorts", sorts);
            session.close();
            
    return SUCCESS;
        }

        
        
    public String findNews()
        
    {
            
    this.init();
            System.out.println(
    "findNews");
            List
    <Newssort> sorts = session.createCriteria(Newssort.class).list();
            Newssort newssort 
    = (Newssort) session.load(Newssort.class, sortid);
            List
    <News> news = newssort.getNews();
            Map request 
    = (Map) ActionContext.getContext().get("request");
            request.put(
    "sorts", sorts);
            request.put(
    "news", news);
            session.close();
            
    return SUCCESS;
        }

    }


    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="dialect">org.hibernate.dialect.MySQLDialect</property>
            
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            
    <property name="connection.url">jdbc:mysql://localhost:3306/news</property>
            
    <property name="connection.username">root</property>
            
    <property name="connection.password">root</property>
            
    <property name="show_sql">true</property>
            
    <!-- 實體類映射 -->
            
    <mapping class="com.vo.News"/>
            
    <mapping class="com.vo.Newssort"/>
        
    </session-factory>
    </hibernate-configuration>    

    struts.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd"
    >
    <struts>
        
    <package name="com" extends="struts-default">
            
    <action name="findNewssort" class="com.web.action.NewsAction" method="findNewssort">
                
    <result name="success">/index.jsp</result>
            
    </action>
            
            
    <action name="findNews" class="com.web.action.NewsAction" method="findNews">
                
    <result name="success">/index.jsp</result>
            
    </action>
        
    </package>
    </struts>    
    web.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi
    ="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation
    ="http://java.sun.com/xml/ns/j2ee 
        http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    >
        
    <filter>
            
    <filter-name>struts2</filter-name>
            
    <filter-class>
                org.apache.struts2.dispatcher.FilterDispatcher
            
    </filter-class>
        
    </filter>
        
    <filter-mapping>
            
    <filter-name>struts2</filter-name>
            
    <url-pattern>/*</url-pattern>
        
    </filter-mapping>
        
    <welcome-file-list>
            
    <welcome-file>prepare.jsp</welcome-file>
        
    </welcome-file-list>
    </web-app>

    前臺有兩個jsp:
    prapare.jsp:
    <%@ page language="java" pageEncoding="GB18030"%>
    <html>
      
    <head>
        
    <title>My JSP 'index.jsp' starting page</title>
        
    <script type="text/javascript">
            window.location 
    = "findNewssort.action";
        
    </script>
      
    </head>
      
    <body>
      
    </body>
    </html>

    index.jsp:
    <%@ page language="java" pageEncoding="GB18030"%>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <html>
      
    <head>
        
    <title>My JSP 'index.jsp' starting page</title>
        
    <script type="text/javascript">
            
    function findNews()
            
    {
                
    var sort = document.getElementById("sort");
                window.location 
    = "findNews.action?sortid=" + sort.value;
            }

        
    </script>
      
    </head>
      
    <body>
          
    <select id="sort" name="sortid" onchange="findNews();">
          
    <option>請選擇</option>
              
    <s:iterator value="#request['sorts']" id="sort" >
                  
    <option value="<s:property value='#sort.nid'/>" ><s:property value="#sort.sort" /></option>
              
    </s:iterator>
          
    </select>
          
    <hr />
          
    <s:iterator value="#request['news']" id="news">
              
    <s:property value="#news.title"/><br />
          
    </s:iterator>
      
    </body>
    </html>

    好了,一切OK,打開瀏覽器測試一切正常.
     源碼可以在我的網盤下載. 下載

    posted on 2007-12-08 21:14 々上善若水々 閱讀(8782) 評論(1)  編輯  收藏

    評論

    # re: 一道Struts面試題  回復  更多評論   

    碰上這種面試題瞬間就是崩潰掉。
    2013-01-30 10:49 | Unmi

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久国产乱子精品免费女| 97在线观免费视频观看| 亚洲第一福利网站| 最近最新MV在线观看免费高清| 亚洲精品伦理熟女国产一区二区| 亚洲国产成人VA在线观看| 性无码免费一区二区三区在线 | 在线亚洲97se亚洲综合在线| 免费国产午夜高清在线视频| 亚洲色www永久网站| 亚洲一二成人精品区| 免费无码肉片在线观看| 一出一进一爽一粗一大视频免费的| 亚洲宅男永久在线| 免费在线观看黄网站| 亚洲免费精彩视频在线观看| 亚洲精品乱码久久久久久蜜桃图片 | 一本久到久久亚洲综合| 99久久免费观看| 色哟哟国产精品免费观看| 99ri精品国产亚洲| 亚洲乱亚洲乱少妇无码| 欧洲乱码伦视频免费| 一区二区三区无码视频免费福利| 亚洲大尺度无码无码专线一区| 久久久久无码精品亚洲日韩| 四虎永久在线精品免费影视 | 亚洲电影免费观看| 特级毛片aaaa免费观看| 久久精品国产亚洲αv忘忧草| 亚洲成av人影院| 免费无码H肉动漫在线观看麻豆| 亚洲欧美日韩中文无线码| 亚洲精品高清国产一久久| 亚洲男人的天堂在线va拉文| 成人免费无码大片a毛片| 2021国内精品久久久久精免费| 中国在线观看免费的www| 国产精品亚洲а∨无码播放麻豆 | 日韩免费高清播放器| 日本激情猛烈在线看免费观看 |