首页 >> 大全

JavaWeb框架-SSH-真品与“赝品”?

2023-12-31 大全 26 作者:考证青年

4、web.xml

这个不要忘记配置。所有的一切,都要通过Web来展现。我们和客户的第一次产品交流。

<web-app><display-name>Archetype Created Web Applicationdisplay-name><filter><filter-name>struts2filter-name><filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilterfilter-class>filter><filter-mapping><filter-name>struts2filter-name><url-pattern>/*url-pattern>filter-mapping><listener><listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>listener><context-param><param-name>contextConfigLocationparam-name><param-value>classpath*:spring.xmlparam-value>context-param>web-app>

5、搞定!庆祝一下(小二,上菜!)

4、核心代码

严格按照面向对象、面向接口、面向切面来设计。

1) model

2) dao

3)

4)

1) model

Blog

package com.hmc.csdn.modle;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;/*** User:Meice* 2017/11/7*/
@Entity
public class Blog {private int blog_id;private String blog_title;private String blog_content;@Id@GenericGenerator(name = "myid",strategy = "native")@GeneratedValue(generator = "myid")public int getBlog_id() {return blog_id;}public void setBlog_id(int blog_id) {this.blog_id = blog_id;}public String getBlog_title() {return blog_title;}public void setBlog_title(String blog_title) {this.blog_title = blog_title;}public String getBlog_content() {return blog_content;}public void setBlog_content(String blog_content) {this.blog_content = blog_content;}@Overridepublic String toString() {return "Blog{" +"blog_id=" + blog_id +", blog_title='" + blog_title + '\'' +", blog_content='" + blog_content + '\'' +'}';}
}

这里为了看效果,先设置了几个简单的字段,后期还会不断完善

2) dao

package com.hmc.csdn.dao;import java.util.List;public interface IBlogDao {void save(Object obj);void del(int id);void update(Object obj);List  list();Object listOne(int id);}

package com.hmc.csdn.dao;
import com.hmc.csdn.modle.Blog;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.awt.print.Book;
import java.util.List;/*** User:Meice* 2017/11/7*/
@Repository
public class BlogDao extends HibernateDaoSupport implements IBlogDao{//继承框架底层封装好的获取sesson的方法,就可以避免在通过sessionFactory获取session了@Resource(name = "sessionFactory")public void setSuperSessionFactory(SessionFactory sessionFactory) {super.setSessionFactory(sessionFactory);}/*** 新增*/public void save(Object obj) {getSessionFactory().getCurrentSession().save(obj);}/*** 删除*/public void del(int id) {System.out.println("删除方法,进来了....");/*getSessionFactory().getCurrentSession().delete(id);*/  // Integer not a  entity!//需要注意,Hibernate删除是直接删除对象,但是根据的是id?所以...需要这么操作.Blog blog = new Blog();blog.setBlog_id(id);getSessionFactory().getCurrentSession().delete(blog);}/*** 修改*/public void update(Object obj) {getSessionFactory().getCurrentSession().update(obj);}/*** 查询所有*/public List list() {List list =   getSessionFactory().getCurrentSession().createQuery("from Blog blog  order by blog.blog_id desc").list();return list;}/*** 查询单个对象*/public Object listOne(int id) {Blog blog =(Blog)  getSessionFactory().getCurrentSession().createQuery("from Blog where blog_id = ?").setParameter(0,id).uniqueResult();return blog;}/*** 定义获取分页总条目数count的方法*/public int countBlogs() {Long countLon =(Long) getSessionFactory().getCurrentSession().createQuery("select count(blog_id) from Blog").uniqueResult();int count =  countLon.intValue();return  count;}/*** 定义分页查询获取博客列表方法getBlogsPager*/public List getBlogsPager(int offset,int pageSize) {List blogs =    getSessionFactory().getCurrentSession().createQuery("from Blog blog  order by blog.blog_id desc").setFirstResult(offset).setMaxResults(pageSize).list();return  blogs;}/*** 运行结果类似:* Hibernate: select blog0_.blog_id as blog_id1_0_, blog0_.blog_content as blog_con2_0_, blog0_.blog_title* as blog_tit3_0_ from Blog blog0_ limit ?*/}

3)

package com.hmc.csdn.service;import java.util.List;public interface IBlogService {void add(Object obj);void del(int id);void update(Object obj);List  list();Object listOne(int id);
}

package com.hmc.csdn.service;
import com.hmc.csdn.dao.BlogDao;
import com.hmc.csdn.modle.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.List;/*** User:Meice* 2017/11/7*/
@Service
public class BlogService implements IBlogService {private BlogDao blogDao;@Resource(name = "blogDao")public void setBlogDao(BlogDao blogDao) {this.blogDao = blogDao;}/*** 发布博客*/public void add(Object obj) {blogDao.save(obj);}/*** 删除博客*/public void del(int id) {blogDao.del(id);}/*** 修改博客*/public void update(Object obj) {blogDao.update(obj);}/*** 博客列表* @return*/public List list() {List list =(List)  blogDao.list();return  list;}/*** 单条博客列表*/public Object listOne(int id) {Blog blog =(Blog)   blogDao.listOne(id);return blog;}/*** 调用博客数目方法*/public int count() {int count =  blogDao.countBlogs();return  count;}/*** 调用分页查询数据方法*/public List getBlogsByPager(int offset,int pageSize) {List blogs = blogDao.getBlogsPager(offset,pageSize);return blogs;}
}

这里的困惑是:定义接口,需要高屋建瓴的能力,否则看得不长远,定义的接口就比较幼稚。

4)

package com.hmc.csdn.controller;
import com.hmc.csdn.modle.Blog;
import com.hmc.csdn.service.BlogService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;/*** User:Meice* 2017/11/7*/@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
@Controller(value = "blogAction")
public class blogAction implements ModelDriven<Blog> {private Blog blog;@Autowiredprivate BlogService blogService;/*** Struts接收参数的第一种方式* 因为没发实现2个ModelDriven*/private int pageIndex=1;//当前页private int count;//博客总条目数private int totalPage;//总页数private int offset;//偏移量,用于limitprivate  int pageSize=15;//每页显示条目private List blogs ;//存放博客,这样就把分页整体作为blogAction的一个属性/*** 博客列表*/public String list() {count =  blogService.count();pageIndex =pageIndex;offset= (pageIndex-1)*pageSize;//执行方法List blogs =(List) blogService.getBlogsByPager(offset,pageSize);//传递值ActionContext.getContext().put("blogs",blogs);return "success";}/*** 跳转到发布博客界面*/public String add() {System.out.println("我是blogAction==> 进来了...");return "success";}/*** 执行发布博客操作*/public String doAdd() {//调用方法blogService.add(blog);//重新跳转到blog_list界面ActionContext.getContext().put("url","blog_doAdd");return "redirectUrl";}/*** 跳转到修改博客界面* @return*/public String update() {//调用方法Blog blogOne =(Blog)  blogService.listOne(blog.getBlog_id());//传递值ActionContext.getContext().put("blog",blogOne);return  "success";}/*** 执行修改操作*/public String doUpdate() {//调用修改方法blogService.update(blog);ActionContext.getContext().put("url","blog_list");return "redirectUrl";}/*** 删除博客*/public String del() {//执行方法blogService.del(blog.getBlog_id());ActionContext.getContext().put("url","blog_list");return "redirectUrl";}//这个很像单例的设计模式public Blog getModel() {if(blog == null) {blog = new Blog();}return blog;}public int getPageIndex() {return pageIndex;}public void setPageIndex(int pageIndex) {this.pageIndex = pageIndex;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public int getTotalPage() {totalPage = (int)Math.ceil((double)count/pageSize);return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getOffset() {offset = (pageIndex-1)*pageSize;return offset;}public void setOffset(int offset) {this.offset = offset;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}
}

这里增加了分页的属性。也在中增加了分页查询及查询博客总条目数的方法。

这里笔者想不到更好的传参方式,已经实现了一个,其他参数就通过属性方式来传递。应该还有更加简洁的办法,后期发现在优化。

5、页面

暂时只是做了个主页面,还有类别管理、评论管理、回收站等模块,后续陆续实现。

list.jsp——显示博客列表

add.jsp——发布博客

.jsp——编辑博客

现在阶段,都还很丑陋,稚气未脱的柑橘。后期逐一美化。欢迎评论斧正!

list.jsp——显示博客列表

<%--User: MeiceDate: 2017/11/7Time: 10:11
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html>
<head><title>Meice_blogtitle><%--这里路径使用基于工程的绝对路径--%><link type="text/css" rel="stylesheet"  href="/CSDN_blog/css/blog.css"><style type="text/css">style><script  src="/js/jquery-3.2.1.jar" type="text/javascript">script><script type="text/javascript">script>
head><body><div id="toolbar"><h2 align="center" style="color: green">Meice_CSDN博客h2>div><div id="blog_whole"><div id="blog_head">div><div id="tabs_head"><h2 id="test">h2><ul id="ul_tab"><li><span><a href="#">文章管理a>span>li><li><span><a href="#">类别管理a>span>li><li><span><a href="#">评论管理a>span>li><li><span><a href="#">回收站a>span>li><li><span style="background-color: #a1ee08"><a href="blog_add">写新文章a>span>li>ul>div><div id="sel_div"><ul><li>类别:  li><li><select style="width: 80px;height: 25px " ><option selected="selected">全部option><option>前言option><option>Javaoption>select>li><li>  类型:  li><li><select style="width: 80px;height: 25px"><option selected="selected">全部option><option>原创option><option>转载option><option>翻译option>select>li>ul>div><div id="list_bog"><table width="98%" align="center" border="1"><caption>caption><thead><tr><th>序号th><th>标题th><th>状态th><th>阅读th><th>评论th><th>评论权限th><th>操作th>tr>thead><tbody><c:forEach var="blogs" items="${blogs}"><tr><td align="center">${blogs.blog_id}td><td>${blogs.blog_title}td><td align="center">审核中td><td>td><td align="center">0td><td align="center"><a href="#">禁止评论a>td><td align="center"><a href="blog_update?blog_id=${blogs.blog_id}">编辑a> |<a href="#">置顶a> |<a href="blog_del?blog_id=${blogs.blog_id}" onclick="return confirm('真的忍心删除么?')">删除a> |<a href="#">分类a> |td>tr>c:forEach><tr><td colspan="7" align="center"><c:if test="${pageIndex>1}"><a href="blog_list?pageIndex=1">首页a><a href="blog_list?pageIndex=${pageIndex-1}">上一页a>c:if><c:forEach var="x" begin="${pageIndex}" end="${pageIndex+9}"><a href="blog_list?pageIndex=${x}">${x}a>c:forEach><c:if test="${pageIndex><a href="blog_list?pageIndex=${pageIndex+1}">下一页a><a href="blog_list?pageIndex=${totalPage}">末页a>c:if>${pageIndex}/${totalPage}td>tr>tbody><tfoot>tfoot>table>div>div><div id="pub_info">div><div>div><div>div>body>
html>

add.jsp——发布博客

<%--User: MeiceDate: 2017/11/7Time: 16:22
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<html>
<head><title>Meice_发布博客title>
head>
<body><form action="blog_doAdd" method="post">标题:<input type="text" name="blog_title"><br/><textarea name="blog_content" cols="50" rows="10">textarea><br/><input type="submit" value="发布">form>
body>
html>

.jsp——编辑博客

<%--User: MeiceDate: 2017/11/7Time: 17:21
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java"  isELIgnored="false" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head><title>Meice_修改博客title>
head>
<body><form action="blog_doUpdate" method="post">标题:<input type="text" name="blog_title" value="${blog.blog_title}"><br/><textarea name="blog_content" cols="50" rows="10"  >${blog.blog_content}textarea><br/><input type="submit" value="修改">form>body>
html>

6、总结

1、解决问题不重要,重要的是解决问题的思路可以复用。比如总是报错,找不到bean,这个时候,就要耐心分析;

不是配置流程有问题,就是哪里写错了。

2、文本域鼠标开始定位没有在开头,下一次实现定位开头,不用回删;

3、如果博客内容过长,数据库字段是自动生成的(255),会报错如下:

Data truncation: Data too long for column 'blog_content' at row 1

待处理;

4、为方便分页,笔者采用页面重定向,反复生成大量数据。发现一个很有意思的现象,那就是重定向访问过多,谷歌、火狐、360浏览器都会自动停止,只有IE还在那里傻乎乎的运行。所以一下生成了1000多条数据,不过也好,分页起来更有“浩瀚”之美!

5、设计接口的工作,有点像企业创始人,他们到达一定阶段,也许、大概、或者就是做类似设计接口之类的事吧。设计接口,要有高瞻远瞩的能力呢,否则定义的接口,不是不完善就是不好用。

7、待完善…

革命尚未成功,同志仍需努力!

小朋友们,下期再会!

关于我们

最火推荐

小编推荐

联系我们


版权声明:本站内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 88@qq.com 举报,一经查实,本站将立刻删除。备案号:桂ICP备2021009421号
Powered By Z-BlogPHP.
复制成功
微信号:
我知道了