xsi="http:
//www.w3.org/2001/XMLSchema-instance"xmlns="xsi:
schemaLocation="id="WebApp_ID"version="3.0">
struts
struts
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts
*.action
Music,java
packagevo;
publicclassMusic{
privateintmusicNo;
privateStringmusicName;
privateStringsinger;
privateStringsize;
privateStringmusicType;
publicintgetMusicNo(){
returnmusicNo;
}
publicvoidsetMusicNo(intmusicNo){
this.musicNo=musicNo;
}
publicStringgetMusicName(){
returnmusicName;
}
publicvoidsetMusicName(StringmusicName){
this.musicName=musicName;
}
publicStringgetSinger(){
returnsinger;
}
publicvoidsetSinger(Stringsinger){
this.singer=singer;
}
publicStringgetSize(){
returnsize;
}
publicvoidsetSize(Stringsize){
this.size=size;
}
publicStringgetMusicType(){
returnmusicType;
}
publicvoidsetMusicType(StringmusicType){
this.musicType=musicType;
}
}
MusicDao.java
packagedao;
importjava.util.List;
importvo.Music;
publicinterfaceMusicDao{
ListqueryAllMusic();
booleandeleteMusic(intmusicID);
}
MusicDaoImpl.java
packagedao;
importjava.sql.Connection;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.ArrayList;
importjava.util.List;
importutil.JDBCutil;
importvo.Music;
publicclassMusicDaoImplimplementsMusicDao{
Connectionconn=null;
Statementst=null;
ResultSetrs=null;
booleanflag=false;
@Override
publicListqueryAllMusic(){
//TODOAuto-generatedmethodstub
Listlist=newArrayList<>();
Stringsql="select*frommusics";/*表musics*/
conn=JDBCutil.getConnection();
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
while(rs.next()){
Musicmusic=newMusic();/*Music是实例vo*/
music.setMusicNo(rs.getInt("MUSIC_ID"));/*MusicNo是实例的字段而MUSIC_ID是数据库的字段*/
music.setMusicName(rs.getString("MUSIC_NAME"));
music.setSinger(rs.getString("SINGER"));
music.setSize(rs.getString("MUSIC_LENGTH"));
music.setMusicType(rs.getString("MUSIC_STYLE"));
list.add(music);
}
}catch(SQLExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
returnlist;
}
@Override
publicbooleandeleteMusic(intmusicID){
//TODOAuto-generatedmethodstub
returnfalse;
}
}
MusicService.java
packageservice;
importjava.util.List;
importvo.Music;
publicinterfaceMusicService{
ListqueryAllMusic();
}
MusicServiecImpl.java
packageservice;
importjava.util.List;
importdao.MusicDao;
importdao.MusicDaoImpl;
importvo.Music;
publicclassMusicServiceImplimplementsMusicService{
MusicDaomusic=newMusicDaoImpl();//父类引用指向子类多态
@Override
publicListqueryAllMusic(){//ADD自动生成的根据方法
//TODOAuto-generatedmethodstub
returnmusic.queryAllMusic();//根据music对象调用Dao层的方法
}
}
Struts.xml
xmlversion="1.0"encoding="UTF-8"?
>
DOCTYPEstrutsPUBLIC
"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.3//EN"
"http:
//struts.apache.org/dtds/struts-2.3.dtd">
--
/jsp/success.jsp
/jsp/error.jsp
MusicManager.java
packageaction;
importjava.util.List;
importservice.MusicServiceImpl;
importvo.Music;
publicclassMusicManager{
privateMusicmusic;//Music实体类
privateListmusicList;//定义集合用来显示数据库的数据
publicStringqueryAllMusic(){//这个是struts中的method方法
musicList=newMusicServiceImpl().queryAllMusic();
if(musicList!
=null){
return"success";
}else{
return"error";
}
}
publicMusicgetMusic(){
returnmusic;
}
publicvoidsetMusic(Musicmusic){
this.music=music;
}
publicListgetMusicList(){
returnmusicList;
}
publicvoidsetMusicList(ListmusicList){
this.musicList=musicList;
}
}
Index.jsp
<%@pagelanguage="java"contentType="text/html;charset=UTF-8"
pageEncoding="UTF-8"%>
DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http:
//www.w3.org/TR/html4/loose.dtd">