更多内容,点击了解: https://how2j.cn/k/mvc/mvc-tutorials/561.html?p=81777
目录
MVC系列教材 (二)- 结合Servlet和JSP 实现查询功能
MVC系列教材 (三)- 结合Servlet和JSP 实现分页功能
MVC系列教材 (四)- 使用Servlet 验证用户是否登陆
MVC系列教材 (一)- 教程
MVC是一种分层的设计模式,本章节从Servlet和JSP的局限性谈起,慢慢引入MVC的概念。
步骤 1 : 仅仅使用Servlet的短处
在Servlet的章节中,使用了编辑Hero的Servlet
根据浏览器提交的id,通过HeroDAO找到对应的Hero,然后在Servlet中组织html显示出来。
可以看到这个Servlet不仅要准备数据,还要准备html。 尤其是准备html,可读性非常差,维护起来也很麻烦
|
步骤 2 : 仅仅使用JSP的短处
因为在Servlet中编写html有这样的短板,所以索性直接在JSP中开发编辑Hero这个功能
这时会发现,虽然编写html方便了,但是写java代码不如在Servlet中那么方便
|
步骤 3 : 结合Servlet和JSP
既然Servlet和JSP都有各自的优势和短板,那么为什么不结合起来扬长避短呢?HeroEditServlet:只用来从数据库中查询Hero对象,然后跳转到JSP页面
|
在request中设置hero
|
服务端跳转到editHero.jsp,因为是服务端跳转,都属于同一次请求,所以可以在editHero.jsp通过request取出来editHero.jsp: 不做查询数据库的事情,直接获取从HeroEditServlet传过来的Hero对象,通过EL表达式把request中的hero显示出来
|
|
步骤 4 : MVC设计模式
上述例子中结合Serlvet和JSP进行数据的显示,就是一种MVC的思想。
M 代表 模型(Model)
V 代表 视图(View)
C 代表 控制器(controller)
模型是什么呢? 模型就是数据,就是dao,bean
视图是什么呢? 就是网页, JSP,用来展示模型中的数据
控制器是什么? 控制器用来把不同的数据,显示在不同的视图上。 在这个例子的,Servlet就是充当控制器的角色,把Hero对象,显示在JSP上。
控制器的作用就是把不同的数据(Model),显示在不同的视图(View)上。
MVC系列教材 (二)- 结合Servlet和JSP 实现查询功能
使用MVC的思想,结合Servlet和JSP进行查询操作
步骤1:实体类Hero步骤2:HeroDAO步骤3:HeroListServlet步骤4:web.xml步骤5:listHero.jsp
步骤 1 : 实体类Hero
|
步骤 2 : HeroDAO
使用在前面教程中用到的HeroDAO用于从数据库查询数据
|
步骤 3 : HeroListServlet
作为控制器的HeroListServlet,其作用就是通过dao获取所有的heros对象,然后放在request中,跳转到listHero.jsp
|
步骤 4 : web.xml
|
步骤 5 : listHero.jsp
作为视图的listHero.jsp,其作用就是把控制器传过来的数据显示出来
|
MVC系列教材 (三)- 结合Servlet和JSP 实现分页功能
随着数据中记录的增多,网页上显示的数据会越来越多。
当多到一定程度的时候,就会影响用户的体验。
解决办法是通过分页技术,一次只显示数据库中的部分数据,如果要看其他数据,可以通过"下一页" "最后一页" 等翻页操作实现
步骤1:首先准备 DAO步骤2:只显示5条数据步骤3:下一页步骤4:上一页步骤5:第一页步骤6:最后一页步骤7:边界处理步骤8:套上Bootstrap
步骤 1 : 首先准备 DAO
在DAO中提供方法
|
start表示开始的个数,count表示取多少条
比如 list(0, 5) , 即表示第一页,每页有5条数据
比如 list(5, 5) , 即表示第二页,每页有5条数据
|
步骤 2 : 只显示5条数据
修改HeroListServlet
|
即表示只获取5条数据
|
步骤 3 : 下一页
HeroListServlet :
通过参数获取start,如果浏览器没有传递参数,就设置为0。
根据start,计算next. next的值就是start+count.
然后把next传递给listHero.jsplistHero.jsp
在最后面增加一个超链
|
start=${next} 从服务器传递过来的next值
|
|
步骤 4 : 上一页
HeroListServlet:
根据start,计算pre. pre的值就是start-count.
然后把pre传递给listHero.jsplistHero.jsp
在下一页前增加一个超链
|
start=${pre} 从服务器传递过来的pre值
|
|
步骤 5 : 第一页
只需要修改listHero.jsp即可
增加
|
因为首页的start永远都是0
|
步骤 6 : 最后一页
HeroListServlet:
在HeroListServlet中计算last
last需要根据总数total和每页有多少条数据count来计算得出。
同时,还要看total是否能够整除count
假设总数是50,是能够被5整除的,那么最后一页的开始就是45
|
假设总数是51,不能够被5整除的,那么最后一页的开始就是50
|
listHero.jsp
在下一页后增加一个超链
|
start=${last} 从服务器传递过来的last值
|
|
步骤 7 : 边界处理
上一页,下一页有一个问题,
如果在第一页点击上一页,就会看不到数据了,因为在第一页的时候,pre=-5,也就导致传递到serlvet的start=-5;
同样的在最后一页的时候,点击下一页,也有类似的问题。
解决办法是进行边界处理:
|
如果pre是负数了,就使用0
|
如果next大于last,就使用last
|
步骤 8 : 套上Bootstrap
|
MVC系列教材 (四)- 使用Servlet 验证用户是否登陆
比如网站提供Hero查询服务,但是前提是用户要登录过才能使用。
如果用户登陆过了,访问listHero,就让用户正常访问,否则就跳转到登陆界面。
这是非常常见的场景,通过使用 session 来实现这个功能。
在处理登录的loginServlet 中使用将用户名保存在session中。
在HeroListServlet 中查看session中是否为空。如果为空,就表示用户没有登陆过,就跳转到登陆页面
步骤1:在LoginServlet 把验证成功的用户加入到 Session步骤2:在HeroListServlet判断Session中是否有数据
步骤 1 : 在LoginServlet 把验证成功的用户加入到 Session
如果用户输入正确的账号密码,就跳转到 listHero,并且把用户名以"userName"放进session
如果用户输入错误的账号密码,就跳转到 login.html,让用户重新登陆
|
步骤 2 : 在HeroListServlet判断Session中是否有数据
|
从session中取出userName,如果是空,就表示用户没有登录,或者登录已经超过了30分钟。 客户端跳转到login.html,让用户重新登陆
|
更多内容,点击了解: https://how2j.cn/k/mvc/mvc-tutorials/561.html?p=81777