入门Struts1第六讲——使用Struts1框架改造在线网上书店
发布日期:2021-06-30 18:04:31
浏览次数:2
分类:技术文章
本文共 4880 字,大约阅读时间需要 16 分钟。
还记得我们之前做过一个简陋的在线网上书店的项目吗?在这篇文章中,我也提供了源码下载地址,如果大家有兴趣的话,可以下载下来,试着运行一下。通过前几讲对Struts1框架的学习,我们算是可以说对于Struts1框架已经有了一定的认识。在本文中,我将会使用Struts1框架改造一下这个项目,看看该框架是怎么应用在项目中的。需要说明一点:我只是针对后台中的分类管理模块进行改造,其他模块可以参照该模块继续进行改造。
搭建Struts1开发环境
首先,在之前的项目中导入Struts1框架的jar包,如下:
然后,在WebRoot/WEB-INF目录下创建Struts1框架的配置文件,即struts-config.xml。其内容如下:这里,只是把该配置文件的架子给搭起来。
接着,在项目中创建一个cn.liayun.resource包,并在该包下新建一个MessageResource.properties资源文件,该资源文件为空即可。 整体的Struts1开发环境搭建好了之后,下面就来改造后台中的分类管理模块。改造后台中的分类管理模块
后台页面中添加分类
首先,在后台左侧导航页面(即left.jsp)修改分类管理模块中的添加分类超链接。
然后,在Struts1框架的配置文件中添加如下配置:接着,要修改添加分类的addcategory.jsp页面,使其提交表单之后,交由Struts1框架处理。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>添加分类页面
紧接着,我们还要修改Struts1框架的配置文件。
最后,在cn.liayun.web.action包中创建一个CategoryAction,用于处理添加分类的请求。package cn.liayun.web.action;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.MappingDispatchAction;import cn.liayun.domain.Category;import cn.liayun.domain.User;import cn.liayun.factory.ServiceFactory;import cn.liayun.service.BusinessService;import cn.liayun.utils.SecurityException;public class CategoryAction extends MappingDispatchAction { public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BusinessService service = ServiceFactory.getInstance().createService((User) request.getSession().getAttribute("user")); try { Category c = new Category(); BeanUtils.copyProperties(c, form); c.setId(UUID.randomUUID().toString()); service.addCategory(c); request.setAttribute("message", "添加成功!!"); } catch (Exception e) { if (e.getCause() instanceof SecurityException) { request.setAttribute("message", e.getCause().getMessage()); } else { e.printStackTrace(); request.setAttribute("message", "添加失败!!"); } } return mapping.findForward("message"); } }
后台页面中查看分类
首先,在后台左侧导航页面(即left.jsp)修改分类管理模块中的查看分类超链接。
然后,在Struts1框架的配置文件中添加名为listcategory的action配置。 紧接着,在CategoryAction中处理显示分类的请求。package cn.liayun.web.action;import java.util.List;import java.util.UUID;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.beanutils.BeanUtils;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.actions.MappingDispatchAction;import cn.liayun.domain.Category;import cn.liayun.domain.User;import cn.liayun.factory.ServiceFactory;import cn.liayun.service.BusinessService;import cn.liayun.utils.SecurityException;public class CategoryAction extends MappingDispatchAction { public ActionForward getAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BusinessService service = ServiceFactory.getInstance().createService((User) request.getSession().getAttribute("user"));//创建ServiceFactory工厂创建service时,需要传递一个用户过去 try { Listlist = service.getAllCategory();//getAllCategory方法调用的是invoke方法,而invoke方法会抛出一个SecurityException异常, //但是为什么这里抓不到这个异常呢? request.setAttribute("categories", list); return mapping.findForward("listcategory"); } catch (Exception e) { if (e.getCause() instanceof SecurityException) { request.setAttribute("message", e.getCause().getMessage()); } else { //如果是別的异常,就在后台记录这个异常 e.printStackTrace(); request.setAttribute("message", "出错了!!!"); } } return mapping.findForward("message"); } public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { BusinessService service = ServiceFactory.getInstance().createService((User) request.getSession().getAttribute("user")); try { Category c = new Category(); BeanUtils.copyProperties(c, form); c.setId(UUID.randomUUID().toString()); service.addCategory(c); request.setAttribute("message", "添加成功!!"); } catch (Exception e) { if (e.getCause() instanceof SecurityException) { request.setAttribute("message", e.getCause().getMessage()); } else { e.printStackTrace(); request.setAttribute("message", "添加失败!!"); } } return mapping.findForward("message"); } }
转载地址:https://liayun.blog.csdn.net/article/details/92798468 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月06日 06时05分00秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
JAVA学习笔记8 - Stream 和 File I/O
2019-04-30
JAVA学习笔记9 - 异常
2019-04-30
JAVA学习笔记10 - 继承
2019-04-30
JAVA学习笔记11 - 接口interface
2019-04-30
JAVA学习笔记12 - 包package
2019-04-30
Android 开发学习笔记 00 - Getting Started
2019-04-30
【学习笔记】Android Activity
2019-04-30
【学习笔记】Android Fragments
2019-04-30
Android使用Retrofit_00_Getting Started
2019-04-30
Android使用Retrofit_01_OAuth2 + GitHub
2019-04-30
Django + REST学习笔记
2019-04-30
【转载】将Ubuntu16.04 中gedit在仅显示一个文件时显示文件名tab
2019-04-30
fstream 对象多次使用时注意clear
2019-04-30
调试 LenaCV 3D Camera (Linux)
2019-04-30
OpenCV杂记 - Mat in C++
2019-04-30
lnmp部署
2019-04-30