新闻web小酌
首页如上
类图如下:
添加新闻的方法(dao):
public boolean Add(News news) {
boolean flag=false;
Connection con =getConnectionData();
QueryRunner queryRunner = new QueryRunner();
String sql="INSERT INTO newscon(newstype,title,author,abstrac ,content,picture) VALUE(?,?,?,?,?,?)";
Object[] paras={news.getNewstype(),news.getTitle(),news.getAuthor(),news.getAbstrac(),news.getContent(),news.getUrl()};
int count=0;
try {
count= queryRunner.update(con,sql,paras);
} catch (SQLException e) {
throw new RuntimeException(e.getMessage(),e);
}
closeAll();
if(count>0){
flag=true;
}
return flag;
}
servlet方法:
public class AddNews extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
if(request.getMethod().equals("GET")){
request.getRequestDispatcher("/newspages/news_add.jsp").forward(request,
response);
}else {
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
boolean flag=false;
try {
List<News> list = (List<News>) FormUtil.assembleObjectList(request,News.class);
News news = list.get(0);
NewsDaoimpl dao=new NewsDaoimpl();
flag = dao.Add(news); } catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (flag) {
response.sendRedirect(path); }else {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
} }
} }
插入标题:
public class AddTitleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
if (request.getMethod().equals("GET")) {
request.getRequestDispatcher("/newspages/news_add.jsp").forward(
request, response);
} else {
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
boolean flag = false;
try {
List<Topic> list = (List<Topic>) FormUtil.assembleObjectList(
request, Topic.class);
Topic topic = list.get(0);
NewsDaoimpl dao = new NewsDaoimpl();
flag = dao.AddTitle(topic); } catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
if (flag) {
response.sendRedirect(path); } else {
request.getRequestDispatcher("/index.jsp").forward(request,
response);
}
}
} }
标题的动态加载:
index:
<c:forEach var="item" items="${requestScope.Toplist}">
<a style="color:blue;font-size:16px;" href='${pageContext.request.contextPath }/DoIndexServlet?id=${item.nid}'>
${item.newstype}
</a>
</c:forEach>
</li>
</ul>
<ul class="classlist">
<c:forEach var="item" items="${requestScope.newsList }">
<li><a href='newspages/news_read.jsp'>${item.title }</a>
<span>
<c:forEach var="item1" items="${TimeList}">
${item1.beginDatetime}
</c:forEach>
</span>
</li>
</c:forEach>
impl:
public List<Topic> AllTitle() { Connection con=getConnectionData();
String sql="select nid,newstype from type";
QueryRunner query=new QueryRunner();
List<Topic> list=null;
try { list= query.query(con, sql,new BeanListHandler<Topic>(Topic.class));
} catch (SQLException e) {
new RuntimeException(e.getMessage()); }
return list;
}
新闻web小酌的更多相关文章
- Java web 小测验
题目要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求八位以上字母.数字组成.(1分) 3性别:要求用单 ...
- 张小龙的野心:用小程序重构web|小程序好处及可能的不足
一:张小龙的野心:用小程序重构web 一 很多年以前,张小龙写了一款软件:Foxmail. 这款软件当年有数百万用户,这是一个相当庞大的量,因为彼时网民也只有千万当量级的规模. 我是一个非常忠实的用户 ...
- WEB 小案例 -- 网上书城(四)
针对于这个小案例我们今天讲解结账操作,也是有关这个案例的最后一次博文,说实话这个案例的博文写的很糟糕,不知道该如何去表述自己的思路,所以内容有点水,其实说到底还是功力不够. 处理思路 点击结账,发送结 ...
- web小知识点
写在前面: 该篇是个人平时对web前端小知识点的总结,会不定时更新...... 如有错误,敬请批评指正. 正文: 1.WWW是World Wide Web的缩写. 2.HTML(Hyper Text ...
- go语言实现一个简单的登录注册web小程序
最近学习golang也有一段时间了,基础差不多学了个大概,因为本人是java程序员,所以对web更感兴趣.根据<go web编程>中的例子改编一个更简单的例子,供新手参考,废话不多说,上菜 ...
- 【WEB小工具】EncodingFilter—设置全局编码
1.我们知道,如果是POST请求,我们需要调用request.setCharacterEncoding("utf-8") 方法来设计编码. public void doGet(Ht ...
- 【WEB小工具】BaseServlet—一个Servlet处理多个请求
package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...
- macaca测试web小例子
上午刚把macaca的环境在公司的电脑上吧web 端的环境给搭建好,于是乎,看看网上的例子,看看官方的文档 https://macacajs.github.io/wd.py/ 可以在这个链接看到原滋原 ...
- WEB 小案例 -- 网上书城(一)
距离上次写博客有两周了吧,最多的原因就是自己期末考试了,上课没听就只能在期末狠狠的复习了,毕竟已经挂科了.当然还是因为自己懒吧!!!废话不多说开始我们今天的正题,网上书城! 一. 新建数据表(MySQ ...
随机推荐
- Laravel框架——自己写的类找不到
composer.json my model files are stored in directory of app\models, therefor "autoload": { ...
- java 反射 动态代理
在上一篇文章中介绍Java注解的时候,多次提到了Java的反射API.与javax.lang.model不同的是,通过反射API可以获取程序在运行时刻的内部结构.反射API中提供的动态代理也是非常强大 ...
- C#程序设计基础——类、对象、方法
类与对象 类 类是一种构造,通过使用该构造,用户可以将其他类型的变量.方法和事件组合在一起,从而创建自定义类型.类就像一个蓝图,它定义类型的数据和行为. 对象 定义类之后,便可通过将类加载到内存中来使 ...
- 如何给div加一个边框border样式
如何给div加一个边框样式? 对div盒子加一个边框样式很简单只需要使用border板块样式即可. 一.虚线与实线边框 边框虚线样式:dashed 边框实现样式:solid border:1px da ...
- 【Linux】鸟哥的Linux私房菜基础学习篇整理(四)
1. VI按键说明.(1)移动光标的方法h或向左箭头:光标向左移动一个字符:j或向下箭头:光标向下移动一个字符:k或向上箭头:光标向上移动一个字符:l或向右箭头:光标向右移动一个字符:进行多次移动,如 ...
- 如果你不好好玩printf
昨天在跟Fiona讨论printf导致程序Crash的问题,就花了点时间看看究竟什么情况下会这样,有兴趣的童鞋可以看看:) 只要是玩过C或者C++的童鞋们,对printf肯定是再熟悉不过了.下面有几个 ...
- 今天愉快的hack小记
今天发生了一件很好玩的事情...那就是WZJ的数据结构(负五)被人水掉了...用的是线段树暴力大发好... XYZ折腾了多长时间的论文题就这么被搞掉了...?窝来维护正义了! 怎么卡呢:让线段树走到叶 ...
- 【转】Android的onCreateOptionsMenu()创建菜单Menu详解
原文网址:http://www.linuxidc.com/Linux/2012-02/55500.htm Android一共有三种形式的菜单: 1.选项菜单(optinosMen ...
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 动态规划——A 最大子段和
A - 最大子段和 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...