FIRE

Nothing is impossible

基于Struts2和Hibernate的简易博客

随语

SpringMVC和SpringBoot两个框架用的真爽,这几天突发奇想想用它的老前辈Struts2和Hibernate搞个项目,要说上手最方便部署的项目应该就是博客了,所以就去网上查了查相关的文档和部署方法,做了个超减配的博客哈哈哈哈~

分析与设计

1. 登录

  • 界面设计:将整个登录窗口居中,并添加image文件夹对登录界面添加背景图片。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    table{
    width: 400px;
    height: 200px;
    border-radius: 20px;
    background:gainsboro;
    font-size:25px;
    color:darkblue;
    text-decoration:none;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    }
    1
    2
    3
    4
    body{
    background-image: url(image/bg2.jpg);
    background-size:cover;
    }
  • 运行设计:用户名和密码判断使用action进行操作,登录时连接数据库的用户表,遍历整个数据库的username和password,使用MySQL的基础语句进行判断操作,对用户名及密码进行正误判定,操作成功将其用户名存入session中,方便后面jsp和action对其操作。

    1
    2
    3
    ResultSet resultSet = stmt.executeQuery("select * from user");
    while(resultSet.next())
    if(username!=null&&resultSet.getString(1).equals(username)&&pwd!=null&&resultSet.getString(2).equals(pwd))
    1
    2
    ActionContext ctx = ActionContext.getContext();
    ctx.getSession().put("user",user);

2. 全部博客内容

界面设计
  • 对表格

    中的进行了样式设计,使用if语句将每行颜色间行相同

    1
    <tr bgcolor=<%if(i%2==1){%>3399FF<%i++;}else{%>white<%i++;}%>>
运行设计
  • 为了显示数据库中的全部博客内容,连接数据库中的用户博客表,包含id,用户名、发布的博客以及发布时间,并对此创建新的类。

    1
    2
    3
    4
    5
    6
    public class UserText {

    private int id;
    private String username;
    private String content;
    private String time;
  • 内容的显示通过ArrayList数组的遍历。考虑到博客首条呈现的一般都是最新发布的博客,所以为达此目的,jsp中先将整个数据库表中的id循环获得里面的最大值(即最新的博客id),然后将id从大到小循环,通过hibernate对id的select方法,将博客内容全部显示。

    1
    2
    int count=size;
    for(int k=count;k>=1;k--)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    public UserText findUserById(int id){
    Session session = HibernateUtil.getSisson();
    try{
    UserText user = (UserText)session.get(UserText.class,id);
    return user;
    }finally{
    if(session!=null)
    session.close();
    }
    }
  • 除此之外,还要限定用户只能对自己的博客内容进行删除修改操作。登录过程中,用户名已存入session,所以只需读取,再用if语句对博客的username进行判断即可。

    1
    2
    <%activeUser=(String)session.getAttribute("user"); 
    session.setAttribute("activeUser",activeUser);%>
    1
    2
    3
    4
    5
    <%if(activeUser.equals(username)){%>
    <td><center><a href="update.jsp?id=${tempId}"><font size=2 color=white>Update</font></a></center></td>
    <td><center><a href="delete?id=${tempId}"><font size=2 color=white><onclick="myFunction()">Delete</onclick></font></a></center></td>
    <%}else{ %>
    <td></td><td></td><%}} %>

3. 发布操作

  • 通过按钮触发“发布”的超链接,进入发布界面,样式沿用了之前的添加背景的方法,更换了图片。标题会显示登录的用户名,且用不同的颜色标注一样是通过session的方法实现。

    1
    <td><center><a href="publish.jsp"><font size="5" color=white>Post Blog</font></a></center></td>
  • 输入框设为textarea,并使用表单提交,然后触发提交的action,通过username和getContent()方法,以hibernate的格式添加至数据库,跳转至登录进入的初始界面。

    1
    <textarea name="content" cols="80" rows="15" align="center"></textarea>
    1
    2
    3
    4
    5
    UserText userText = new UserText();
    UserTextDao dao = new UserTextDao();
    userText.setUsername((String)ctx.getSession().get("activeUser"));
    userText.setContent(getContent());
    dao.save(userText);

4. 修改操作

  • 根据登录者的不同,对自己的个人博客进行修改。点击update,触发跳转,跳转的同时传递每条博客的id(遍历的同时把id以EL语言的形式固定在每个触发按钮操作上),由id使用hibernate的selectId的方法将具体博客的内容以EL语言的格式呈现在修改框。修改入库的操作就与发布的操作相一致,样式方面同样与发布相似。

    1
    <a href="update.jsp?id=${tempId}">
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <%
    int id = Integer.parseInt(request.getParameter("id"));
    session.setAttribute("id",id);
    UserText userText = new UserText();
    UserTextDao dao = new UserTextDao();
    userText = dao.findUserById(id);
    String content=userText.getContent();
    session.setAttribute("content",content);
    %>
    <textarea name="content" cols="80" rows="15" align="center">${content}</textarea>

5. 删除操作

  • 根据登录者的不同,对自己的个人博客进行删除。点击delete触发action,跳转的同时传递每条博客的id(遍历的同时把id以EL语言的形式固定在每个触发按钮操作上)

    1
    <a href="delete?id=${tempId}">
  • 由id使用hibernate的selectId的方法将具体博客的内容从数据库中删除。删除伴随弹窗。

    1
    2
    3
    4
    5
    int id=getId();
    UserText userText = new UserText();
    UserTextDao dao = new UserTextDao();
    userText=dao.findUserById(id);
    dao.delete(userText);
    1
    2
    3
    4
    <script>
    function myFunction()
    { alert("删除成功!");}
    </script>

6. 退出

  • 退出即跳转,点击触发退出的action,将session的内容进行清空,并跳回至登录界面。

    1
    2
    ActionContext ctx = ActionContext.getContext();
    ctx.getSession().clear();

成品

登陆界面

全部用户博客

写博客

-------- 🎈本文结束 感谢阅读🎈 --------
感謝老闆支持!