* 得到所有的新闻类别
*/
public List<Newstype> getAllNewsType() {
Session session=null;
List<Newstype> list=null;
Newstype news=null;
List<Newstype> list2=new ArrayList<Newstype>();
try {
//得到连接
session=MySessionFactory.getSession();
//sql语句
String sql="select * from newstype";
// 语句对象
Query query=session.createSQLQuery(sql).addEntity(Newstype.class);
//集合
list=query.list();
//遍历集合 放到另一个集合中,用于gson的调用
for(int i=0;i<list.size();i++){
news= new Newstype();
news.setId(list.get(i).getId());
news.setNewstype(list.get(i).getNewstype());
list2.add(news);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭连接
session.close();
}
return list2;
---------------------------------------------------------------------------------------------------------------------------------------------------
以下是servlet中的方法
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//导包
//创建磁盘对象(核心类)
DiskFileItemFactory disk=new DiskFileItemFactory();
//创建上传servlet类
ServletFileUpload upload=new ServletFileUpload(disk);
try {
//重新解析请求request对象,因为包含了图片字段
List<FileItem> list=upload.parseRequest(request);
//定义一个hashmap把下面的值放进去
HashMap<String ,String > parmars=new HashMap<String, String>();
//遍历
for(FileItem f:list){
if(f.isFormField()){
//把字段名和值放到hashmap中去
parmars.put(f.getFieldName(), f.getString("UTF-8"));
}
else{
//动态获取
String path=getServletContext().getRealPath("/file");
//获取文件数据
byte []data=f.get();
//动态获取用户上传的图片名称
String filename=f.getName();
parmars.put(f.getFieldName(), filename);
FileOutputStream out2=new FileOutputStream(path+"/"+filename);
out2.write(data);
//刷新并关闭
out.flush();
out.close();
}
}
//遍历hashmap
//创建实现类对象
INewsDaoImpl impl=new INewsDaoImpl();
//创建新闻类型对象
Newstype type=new Newstype();
//把得到的类型放到新闻类型中
type.setNewstype(parmars.get("type"));
//调用构造方法
Newscontent news=new Newscontent(type, parmars.get("title"), parmars.get("content"), parmars.get("autor"), parmars.get("image"));
//调用插入方法将新闻对象添加到数据库中
int r=impl.addNews(news);
if(r>0){
out.print("<script>alert('添加成功');location.href='list.jsp'</script>");
}else{
out.print("<script>alert('添加失败');location.href='addnews.jsp'</script>");
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
--------------------------------------------------------------------------------------------------------------------------------------------
以下是jsp页面中的代码
<script type="text/javascript">
$(function(){
alert(1)
$.ajax({
type:"post",
url:"DoNewsType.do",
data:"",
success:function(msg){
//此时从后台传过来的msg为字符串,要转换成object类型才能调用属性
msg=eval("("+msg+")"); //json字符串变为json对象
$("select").html("") //置空select
for(var i=0;i<msg.length;i++){
str="<option value="+msg[i].id+">"+msg[i].newstype+"</option>";
//追加到select中
$("select").append(str);
}
}
})
})
</script>
</head>
<body>
<form action="DoAddNews.do" method="post" enctype="multipart/form-data">
<div id="one">
<h2>添 加 新 闻</h2>
<table cellpadding="0" cellspacing="0">
<tr><td class="left">新闻标题:</td><td><input type="text" name="title" /></td></tr>
<tr><td>所属类别:</td><td><select name="type"></select></td></tr>
<tr><td class="left">新闻内容:</td><td><textarea name="content" ></textarea></td></tr>
<tr><td class="left">新闻图片:</td><td><input type="file" name="image" /></td></tr>
<tr><td class="left">发布人:</td><td><input type="text" name="autor" /></td></tr>
<tr><td class="left"><input type="submit" value="提交" /></td><td> <input type="reset" value="重置" /></td></tr>
</table>
</div>
</form>
</body>