一道Struts面试题
有两张表
一张为新闻类别表
有2个字段:
nid(pk) sort
有一张新闻内容表
有三个字段
cid(pk) nid(fk) title content
要求通过下拉列表框的方法选择新闻类别然后显示该类别的新闻标题(在当前页中显示)
我是用Struts2+Hibernate3.2+JPA实现的.
数据库脚本:
droptableifexistsnewssort;
createtablenewssort
(
nidintprimarykeyAUTO_INCREMENT,
sortvarchar(50)
);
droptableifexistsnews;
createtablenews
(
cidintprimarykeyAUTO_INCREMENT,
titlevarchar(50)notnull,
contentvarchar(500)notnull,
nidintnull
);
insertintonewssortvalues(null,'娱乐');
insertintonewssortvalues(null,'时事');
insertintonewsvalues(null,'好事','好事连连哈哈',1);
insertintonewsvalues(null,'坏事','坏事不断',1);
insertintonewsvalues(null,'爱情是什么','爱情是什么啊,还没知道呢',2);
insertintonewsvalues(null,'什么啊','测试内容',2);
select*fromnews;
select*fromnewssort;
两个VO类:
News.java:
importjava.io.Serializable;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name="news")
publicclassNewsimplementsSerializable
{
privateIntegercid;
privateStringtitle;
privateStringcontent;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
publicIntegergetCid()
{
returncid;
}
publicvoidsetCid(Integercid)
{
this.cid=cid;
}
publicStringgetTitle()
{
returntitle;
}
publicvoidsetTitle(Stringtitle)
{
this.title=title;
}
publicStringgetContent()
{
returncontent;
}
publicvoidsetContent(Stringcontent)
{
this.content=content;
}
}
Newssort.java:
importjava.io.Serializable;
importjava.util.ArrayList;
importjava.util.List;
importjavax.persistence.Entity;
importjavax.persistence.GeneratedValue;
importjavax.persistence.GenerationType;
importjavax.persistence.Id;
importjavax.persistence.JoinColumn;
importjavax.persistence.OneToMany;
importjavax.persistence.Table;
importorg.hibernate.annotations.LazyCollection;
importorg.hibernate.annotations.LazyCollectionOption;
@SuppressWarnings("serial")
@Entity
@Table(name="newssort")
publicclassNewssortimplementsSerializable
{
privateIntegernid;
privateStringsort;
privateList<News>news=newArrayList<News>();
@OneToMany
@JoinColumn(name="nid")
@LazyCollection(LazyCollectionOption.FALSE)
publicList<News>getNews()
{
returnnews;
}
publicvoidsetNews(List<News>news)
{
this.news=news;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
publicIntegergetNid()
{
returnnid;
}
publicvoidsetNid(Integernid)
{
this.nid=nid;
}
publicStringgetSort()
{
returnsort;
}
publicvoidsetSort(Stringsort)
{
this.sort=sort;
}
}
写个测试类先测试一个持久层操作:
importjava.util.Iterator;
importorg.hibernate.Session;
importorg.hibernate.cfg.AnnotationConfiguration;
importorg.junit.After;
importorg.junit.Before;
importcom.vo.News;
importcom.vo.Newssort;
publicclassTest
{
privateSessionsession;
@Before
publicvoidsetUp()
{
session=newAnnotationConfiguration().configure().buildSessionFactory().openSession();
}
@After
publicvoidtearDown()
{
session.close();
}
@SuppressWarnings("unchecked")
@org.junit.Test
publicvoidtestFind()
{
@SuppressWarnings("unused")
//List<Newssort>newssort=session.createCriteria(Newssort.class).list();
Newssortnewssort=(Newssort)session.load(Newssort.class,2);
for(Iterator<News>i=newssort.getNews().iterator();i.hasNext();)
{
Stringtitle=i.next().getTitle();
System.out.println(title);
}
}
}
好了写Action
NewsAction:
importjava.util.List;
importjava.util.Map;
importorg.hibernate.Session;
importorg.hibernate.cfg.AnnotationConfiguration;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
importcom.vo.News;
importcom.vo.Newssort;
@SuppressWarnings({"serial","unchecked"})
publicclassNewsActionextendsActionSupport
{
privateSessionsession;
privateIntegersortid;
publicIntegergetSortid()
{
returnsortid;
}
publicvoidsetSortid(Integersortid)
{
this.sortid=sortid;
}
publicvoidinit()
{
session=newAnnotationConfiguration().configure()
.buildSessionFactory().openSession();
}
publicStringfindNewssort()
{
this.init();
List<Newssort>sorts=session.createCriteria(Newssort.class).list();
Maprequest=(Map)ActionContext.getContext().get("request");
request.put("sorts",sorts);
session.close();
returnSUCCESS;
}
publicStringfindNews()
{
this.init();
System.out.println("findNews");
List<Newssort>sorts=session.createCriteria(Newssort.class).list();
Newssortnewssort=(Newssort)session.load(Newssort.class,sortid);
List<News>news=newssort.getNews();
Maprequest=(Map)ActionContext.getContext().get("request");
request.put("sorts",sorts);
request.put("news",news);
session.close();
returnSUCCESS;
}
}
hibernate.cfg.xml:
<!DOCTYPEhibernate-configurationPUBLIC
"-//Hibernate/HibernateConfigurationDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="connection.url">jdbc:mysql://localhost:3306/news</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">root</property>
<propertyname="show_sql">true</property>
<!--实体类映射-->
<mappingclass="com.vo.News"/>
<mappingclass="com.vo.Newssort"/>
</session-factory>
</hibernate-configuration>
struts.xml:
<!DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<packagename="com"extends="struts-default">
<actionname="findNewssort"class="com.web.action.NewsAction"method="findNewssort">
<resultname="success">/index.jsp</result>
</action>
<actionname="findNews"class="com.web.action.NewsAction"method="findNews">
<resultname="success">/index.jsp</result>
</action>
</package>
</struts>
web.xml:
<web-appversion="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:
<html>
<head>
<title>MyJSP'index.jsp'startingpage</title>
<scripttype="text/javascript">
window.location="findNewssort.action";
</script>
</head>
<body>
</body>
</html>
index.jsp:
<%@tagliburi="/struts-tags"prefix="s"%>
<html>
<head>
<title>MyJSP'index.jsp'startingpage</title>
<scripttype="text/javascript">
functionfindNews()
{
varsort=document.getElementById("sort");
window.location="findNews.action?sortid="+sort.value;
}
</script>
</head>
<body>
<selectid="sort"name="sortid"onchange="findNews();">
<option>请选择</option>
<s:iteratorvalue="#request['sorts']"id="sort">
<optionvalue="<s:propertyvalue='#sort.nid'/>"><s:propertyvalue="#sort.sort"/></option>
</s:iterator>
</select>
<hr/>
<s:iteratorvalue="#request['news']"id="news">
<s:propertyvalue="#news.title"/><br/>
</s:iterator>
</body>
</html>
好了,一切OK,打开浏览器测试一切正常.
源码可以在我的网盘下载. 下载
一道Struts面试题相关推荐
- 一道Python面试题,据说大部分人都中招了,纷纷开始怀疑自己
无意间,看到这么一道Py无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun(): temp = [lambda x : i*x for i in range(4)] ...
- 历史上最简单的一道Java面试题,但无人能通过
作者:方志宏 来源:zhuanlan.zhihu.com/p/57859872 这可能是历史上最简单的一道java面试题了. 题目很简单,完成代码,判断一个整数是否是奇数: public boolea ...
- 浅入深谈:一道Python面试题,让我明白了殊途同归,却开始怀疑自己
无意间,看到这么一道Python面试题:以下代码将输出什么? 脑中默默一想,这还用说么,肯定是: 0 2 4 6 最后一看答案,竟然是: 6 6 6 6 于是带着怀疑的心态(其实是不服输,不认错),打 ...
- 一道Python面试题
无意间,看到这么一道Python面试题:以下代码将输出什么? def testFun(): temp = [lambda x : i*x for i in range(4)] return temp ...
- 字节跳动一道Python面试题
最近一直在自学Python,同时还在上班,而且还有很多的事情要做,所以我的计划是一年内学会,目前还是在坚持当中,今天分享一道字节跳动的一道Python面试题,面试时对答题时间有要求且不能切换屏幕,我感 ...
- 一道HashSet面试题引发的蝴蝶效应
没错,我又借着"面试题"的名头来搞事情了,今天要说的是 HashSet ,而这确实是一个实际面试中遇到的问题.当时的场景大概是这样的,面试官在了解了你的知识广度以后,决心来考察一番 ...
- 数位DP--由一道微软笔试题引起
前天晚上,一位研三的学长突然跑到我们宿舍,问我们一道微软笔试题.给你一个整数n,求出1到n这个区间范围内包含数字0的个数,例如当n=10的时候就只有10包含0,输出1,n=90就输出9.唯一的要求是此 ...
- 输入url到页面加载都发生了什么事情?(一道软件测试面试题)
文章首发于公众号:软件测试er 输入url到页面加载都发生了什么事情?这是一道软件测试面试题,且相关知识其实还有多种问法: Question 1请讲一下tcp三次握手 OK等你把这个记好了,下次面试: ...
- 外企一道 SQL 面试题,刷掉 494 名候选人
点击蓝色"有关SQL"关注我哟 加个"星标",天天与10000人一起快乐成长 图 | Lenis 叮叮... 清脆的微信声,把我从梦中唤醒.早已习惯起床之后, ...
最新文章
- 有别于BATJ,滴滴的中台数据体系建设怎么另辟蹊径?
- 微信服务号 微信支付开发
- 数据采集技术python网络爬虫答案_高校邦网络数据采集与Python爬虫【带实验】章节答案...
- appium for mac 安装与测试ios说明
- 无人值守安装之cdrom_无人值守安装
- springboot中注入FilterRegistrationBean不生效原因
- php asserttrue,PHP8.0的新特性:错误处理方面已经改进
- python爬虫06
- Zabbix二次开发_03api列表
- 在Node.js中操作文件系统(一)
- Reading Digits(2016southeastern Europe. D题)题解
- 小程序常用ui库 组件库
- Java 中的十大排序算法
- 移动硬盘损坏,数据能恢复吗
- java达内项目_达内IT学院举办Java互联网架构师项目峰会
- Java 抛出异常【throw】
- 如何取消windows XP登录框,让其自动登录
- xcode 编译 c
- VOLO Vision Outlooker for Visual Recognition论文分析
- 数据模型与数据库之间的关系
热门文章
- html 日期判断周几,javascript怎么根据日期判断星期几?
- 交换机短路_交换机日常怎么运行维护?一文告诉你
- 剑指offer面试题[14]-调整数组顺序使奇数位于偶数前面
- java21天打卡day20-集合
- Python21天打卡Day15-21 函数+参数
- dl360 g7安装linux,HPDL360G7服务器安装说明.ppt
- oracle 频繁 tm tx,oracle频繁出现TX/TM锁问题
- 安装ps时无法验证订阅状态_免费申请office E5开发者订阅,附无限续期+私人网盘教程...
- python复制文件夹不阻塞_11.python并发入门(part14阻塞I/O与非阻塞I/O,以及引入I/O多路复用)...
- 打线上包 测试_vue-cli项目分别打测试包(test)和生产包(prod)