一道Struts面试题

article/2025/9/30 13:09:55

 

题目是这样的
有两张表
一张为新闻类别表
有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 ( 50 not   null ,
  content 
varchar ( 500 not   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,打开浏览器测试一切正常.
 源码可以在我的网盘下载. 下载

http://chatgpt.dhexx.cn/article/sGNARUod.shtml

相关文章

Java面试----2018年最新Struts2面试题

1、描述Struts2的工作原理 答:客户端发送请求--》请求经过一系列过滤器--》FilterDispatcher通过ActionMapper来决定这个Request需要调用哪个Action --》FilterDispatcher把请求的处理交给ActionProxy--》通过ConfigurationManager询问Struts配置文件&#xff08;Struts.xml&a…

Struts2面试问题

转载自 Struts2面试问题 1.什么是Struts2&#xff1f; Apache Struts2是一个用Java构建Web应用程序的开源框架。Struts2基于OpenSymphony WebWork框架。它从Struts1中得到了很大的改进&#xff0c;使其更加灵活&#xff0c;易于使用和扩展。Struts2的核心组件是Action&…

查看es的tcp和http端口

1、登录linux部署服务器&#xff0c;用命令查找配置文件elasticsearch.yml&#xff0c;如图 find -name elasticsearch.yml 2、进到elasticsearch.yml文件的目录 3、查看tcp&#xff0c;http端口

w10查看端口_Windows 10系统如何查看已打开的端口

最近Win10用户反映&#xff0c;电脑很经常中病毒&#xff0c;用户表示并没有在电脑上插入U盘&#xff0c;也没有打开不安全的网页&#xff0c;但就是一直中毒&#xff0c;这让用户非常苦恼。其实&#xff0c;出现这一问题&#xff0c;可能与电脑开启了一些端口有关&#xff0c;…

使用frp端口映射实现内网穿透(SSH、HTTP服务)

使用frp端口映射实现内网穿透(SSH、HTTP服务) 一、下载 通过内网穿透的原理和实现方式的学习我们已经明白了内网穿透的原理&#xff0c;想要实现内网穿透就需要让内网实现与具有公网IP的设备进行绑定。 我们这里使用frp&#xff08;一个专注于内网穿透的高性能的反向代理应用…

rinetd端口转发工具

前言 环境&#xff1a;Centos7.9 rinetd.tar.gz 在生产环境中&#xff0c;为了网络安全&#xff0c;我们需要进行端口转发&#xff0c;而rinetd是一款很好用的端口转发工具&#xff0c;下面我们就来讲解一下如何使用rinetd来实现端口转发。rinetd的下载地址&#xff1a;http:…

电脑端口详解

计算机端口号总数&#xff1a;65535&#xff0c;一般用到的是1~65535&#xff0c;0一般不使用 0-1023&#xff1a; 系统端口&#xff0c;也叫公认端口&#xff0c;这些端口只有系统特许的进程才能使用&#xff1b;1024~65535为用户端口&#xff1a; 1024-5000&#xff1a; 临时…

HTTP服务占用80端口的解决办法,找出占用80的元凶。

电脑没有运行web服务&#xff0c;但是80端口被占用&#xff0c;导致运行使用80端口的软件的时候提示80端口监听失败。 网络上搜索一般给的办法是 net stop HTTP&#xff0c;把windows的http API禁用&#xff0c;这样确实80端口没有占用了&#xff0c;但是所有依赖http的服务就…

python端口扫描

文章目录 python网络编程socket函数服务器端套接字函数客户端套接字函数公共用途套接字 C/S架构实践 编写端口扫描器多线程端口扫描2.0 python网络编程 socket又称“套接字”&#xff0c;应用程序通常通过“套接字”向网络发出请求或者应答请求。使主机间或者一台计算机上的进…

https默认端口443_为什么选择80作为默认HTTP端口,选择443作为默认HTTPS端口?

https默认端口443 While many of us are familiar with various ports being assigned for specific purposes or uses, we may not know the particular reason why they were chosen. Today’s SuperUser Q&A post has the answers to a curious reader’s questions. 尽…

HTTP协议

目录 前置 1.网络划分 1&#xff09;局域网 2&#xff09;广域网 2.IP地址和端口号 1&#xff09;IP地址 2&#xff09;端口号 3.网络通信 一&#xff0c;概念 1.HTTP 2.二进制数据和文本 二&#xff0c;抓包 1.网络抓包 2.URL&#xff08;统一资源定位器&#…

C语言字符串数组赋值错误

直接上代码&#xff1a; #include"stdio.h"int main(){char a[10];for(int i 0; i<10;i){printf("第%d个&#xff1a;",i);scanf("%c",&a[i]);}return 0; }经典的错误&#xff0c;标准的零分&#xff01; 这样的赋值手法&#xff0c;是…

c 语言定义2维字符串数组赋值,二维数组赋值字符串 c 语言 二维字符串数组赋值问题...

C语言中二维字符数组应该怎样赋值&#xff1f; c语言二维数组如何定义字符串&#xff1f;&#xff1f;&#xff1f;&#xff1f;急。。。 二维字符数组的定义格式为&#xff1a;char 数组名[第一维大小][第二维大小]; 例如&#xff1a;char c[3][10]; //定义了一个3行10列的二维…

关于中字符串常量给字符数组赋值

结论 在定义字符数组时&#xff0c;可以同时初始化字符数组&#xff0c;即用字符串常量给字符数组赋值在定义完之后&#xff0c;则不允许用字符串常量给字符数组赋值。如需赋值&#xff0c;则使用strcpy()函数&#xff08;需包含头文件string.h&#xff09; #include <std…

C语言:字符数组赋值

字符数组&#xff1a;C语言学习中较麻烦的部分&#xff0c;C语言中没有字符串这种类型的数组&#xff0c;字符串只能存储在字符型数组中。 1、定义时直接赋值 char a[10]{"I LOVE C"};//可以直接省去花括号直接写成char a[10]I LOVE C"; 注意&#xff1a;此处的…

【C#】 Convert.ToInt16 、Convert.ToInt32、Convert.ToInt64 区别

一般写程序是用的都是Convert.ToInt32&#xff0c;为什么呢&#xff1f; 1.Convert.ToInt是数据类型转换成int类型 2. 有三种方法toint16,toint32,toint64 int16-数值范围&#xff1a;-32768 到 32767 int32-数值范围&#xff1a;-2,147,483,648 到 2,147,483,647 …

C# 中int short Int16 Int32 Int64详解

Java中没有Int32,Int64,,只有int,short,long Java中int就代表Int32 ,short就代表Int16&#xff0c;long就代表Int64 首先&#xff0c;几个基本的关键字&#xff1a; Int16 short, 占2个字节. -32768 ~ 32767 Int32 int, 占4个字节. -2147483648 ~ 2147483647 Int64 lon…

【C++】INT32_MAX

INT32_MAX INT32_MAX可以把它认为在limits.h下面的一个宏。关于宏&#xff0c;可以参考这篇文章&#xff0c;直接点击&#xff01;其实就是一个别称&#xff01;

python中int的取值范围_int32的取值范围是多少?

int32的数值取值范围为“-2147483648”到“2147483647”;而int64的数值取值范围为“-9223372036854775808”到“9223372036854775808”。 int32的取值范围 计算机中32位int类型变量的范围,其中int类型是带符号整数。 正数在计算机中表示为原码,最高位为符号位: 1的原码为000…

c语言unsigned int 范围,unsigned int 32比特数据范围为-2147

提到unsigned,大家应该都了解,有朋友问c语言中unsigned什么意思,还有人想问c语言中的unsigned是什么意思,这到底是咋回事?事实上unsigned呢,下面是小编推荐给大家的unsigned int,下面我们一起来看看吧! unsigned int 一、指代不同 1、int:定义整数类型变量的标识符。 …