public void save() { String title = request.getParameter("title"); String pubren = request.getParameter("pubren"); String pubtime = request.getParameter("pubtime"); String laiyuan=request.getParameter("laiyuan"); String dcontent = request.getParameter("dcontent"); String tupian2 = request.getParameter("tupian2"); String lanmuid = request.getParameter("lanmuid"); String lanmuming = request.getParameter("lanmuming"); String tuijian=request.getParameter("tuijian"); String hot=request.getParameter("hot"); String zuixin=request.getParameter("zuixin"); String zhaiyao=request.getParameter("zhaiyao"); String style=request.getParameter("style"); SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd"); Xinxi xinxi = new Xinxi(); xinxi.setTitle(title == null ? "" : title); xinxi.setPubren(pubren == null ? "" : pubren); xinxi.setPubtime(new Date()); xinxi.setHot(hot!=null?1:0); xinxi.setTuijian(tuijian!=null?1:0); xinxi.setZuixin(zuixin!=null?1:0); xinxi.setClickcount(0); xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao); xinxi.setDcontent(dcontent == null ? "" : dcontent); xinxi.setTupian2(tupian2 == null ? "" : tupian2); xinxi.setAgainstcount(0); xinxi.setAgreecount(0); xinxi.setLanmuid(new Integer(lanmuid)); xinxi.setLanmuming(lanmuming == null ? "" : lanmuming); DALBase.save(xinxi); try { if(style!=null&&style.equals("admin")) response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid()); if(style!=null&&style.equals("huiyuan")) response.sendRedirect("../e/myxinximanager.jsp?seedid=m2"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /****************************************************** *********************** 内部附件支持********************* ***public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) { DALBase.delete("attachement", MessageFormat.format( " where belongid=''{0}'' and belongtable=''xinxi'' ", belongid)); String[] photos = request.getParameterValues("fileuploaded"); if (photos == null) return; for (int i = 0; i < photos.length; i++) { Attachement a = new Attachement(); a.setType("images"); a.setPubtime(new Date()); a.setBelongfileldname("id"); a.setFilename(photos[i]); a.setBelongid(belongid); a.setBelongtable("xinxi"); a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename()); a.setTitle(a.getFilename()); DALBase.save(a); } }解释每一行代码
时间: 2024-01-17 22:27:00 浏览: 65
java request.getParameter中文乱码解决方法
这是一段Java代码,根据传入的HTTP请求参数保存一个信息(Xinxi)到数据库中。下面是每行代码的解释:
```java
public void save() { // 定义一个公有方法save()
// 从请求参数中获取信息标题、发布者、发布时间、来源、正文内容、图片、栏目ID、栏目名称、推荐、热点和最新等信息
String title = request.getParameter("title");
String pubren = request.getParameter("pubren");
String pubtime = request.getParameter("pubtime");
String laiyuan=request.getParameter("laiyuan");
String dcontent = request.getParameter("dcontent");
String tupian2 = request.getParameter("tupian2");
String lanmuid = request.getParameter("lanmuid");
String lanmuming = request.getParameter("lanmuming");
String tuijian=request.getParameter("tuijian");
String hot=request.getParameter("hot");
String zuixin=request.getParameter("zuixin");
String zhaiyao=request.getParameter("zhaiyao");
String style=request.getParameter("style");
// 定义一个日期格式化对象,格式为"yyyy-MM-dd"
SimpleDateFormat sdfxinxi = new SimpleDateFormat("yyyy-MM-dd");
// 创建一个信息对象
Xinxi xinxi = new Xinxi();
// 将获取到的信息设置到信息对象中
xinxi.setTitle(title == null ? "" : title);
xinxi.setPubren(pubren == null ? "" : pubren);
xinxi.setPubtime(new Date()); // 设置发布时间为当前时间
xinxi.setHot(hot!=null?1:0); // 如果参数hot不为空,则将热点属性设为1,否则设为0
xinxi.setTuijian(tuijian!=null?1:0); // 如果参数tuijian不为空,则将推荐属性设为1,否则设为0
xinxi.setZuixin(zuixin!=null?1:0); // 如果参数zuixin不为空,则将最新属性设为1,否则设为0
xinxi.setClickcount(0); // 设置点击次数为0
xinxi.setZhaiyao(zhaiyao==null?"":zhaiyao);
xinxi.setDcontent(dcontent == null ? "" : dcontent);
xinxi.setTupian2(tupian2 == null ? "" : tupian2);
xinxi.setAgainstcount(0); // 设置反对次数为0
xinxi.setAgreecount(0); // 设置赞同次数为0
xinxi.setLanmuid(new Integer(lanmuid)); // 将栏目ID设置到信息对象中
xinxi.setLanmuming(lanmuming == null ? "" : lanmuming);
// 调用数据访问层的save()方法保存信息对象到数据库中
DALBase.save(xinxi);
try {
// 根据请求参数中的style值进行不同的重定向操作
if(style!=null&&style.equals("admin"))
response.sendRedirect("xinximanager.do?actiontype=get&seedid=201&lanmuid="+xinxi.getLanmuid());
if(style!=null&&style.equals("huiyuan"))
response.sendRedirect("../e/myxinximanager.jsp?seedid=m2");
} catch (IOException e) {
e.printStackTrace();
}
}
// 定义一个公有方法attachements()用于支持内部附件
public void attachements(HttpServletRequest request, HttpServletResponse response, String belongid) {
// 删除已存在的附件
DALBase.delete("attachement", MessageFormat.format(" where belongid=''{0}'' and belongtable=''xinxi'' ", belongid));
// 获取请求参数中的上传的图片文件名
String[] photos = request.getParameterValues("fileuploaded");
if (photos == null)
return;
// 遍历图片文件名数组,创建附件对象并保存到数据库中
for (int i = 0; i < photos.length; i++) {
Attachement a = new Attachement();
a.setType("images");
a.setPubtime(new Date()); // 设置附件发布时间为当前时间
a.setBelongfileldname("id");
a.setFilename(photos[i]);
a.setBelongid(belongid);
a.setBelongtable("xinxi");
a.setUrl(SystemParam.getSiteRoot() + "/upload/temp/" + a.getFilename());
a.setTitle(a.getFilename());
DALBase.save(a);
}
}
```
阅读全文