Struts2学习笔记(第一天)

    xiaoxiao2021-04-12  35

    1.今日任务

    ·使用struts2完成客户列表查询

    2.相关知识

    2.1 Struts2框架的概述

    2.1.1 什么是struts2

    Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2Struts的下一代产品,是在 struts 1WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1Struts 2有着太大的变化,但是相对于WebWorkStruts 2的变化很小。

    2.1.2 Struts2Struts1的区别

    Struts2Struts1没有任何联系.Struts2内核是webwork的内核.

    2.2搭建Struts2框架

    2.2.1 下载Struts2的开发包

    官网: https://struts.apache.org/

    2.2.2 解压Struts2的开发包

    * apps      :Struts2提供一些案例

    * docs       :Struts2开发文档.

    * lib  :Struts2的开发的jar

    * src          :Struts2的源码

    2.2.3 导包

    可以看到lib目录下有100多个jar包,实际开发中我们用不到这么多,打开解压目录下的apps文件夹可以看到几个war文件:

    这些是Struts2官方给我们提供的案例,用压缩软件解压struts2-blank.war文件,找到WEB-INF目录下的lib文件:

    这些包就是Struts2的基本开发包了,那么这些包都有什么含义呢?

    2.2.4 编写一个Action

    public class HelloAction { public String hello(){ System.out.println("hello world!"); return "success"; } }

     

    2.2.5 编写配置文件

    找到刚才解压的struts2-blank文件进入WEB-INF\classes目录下可以找到struts.xml文件,将其拷贝到自己项目的src目录下。

     

    在编写配置文件之前先配置struts.xml的离线提示。

    展开struts2-core-2.3.32jar

    往下拉可以看到dtd文件

    因为jar包里的文件不允许被拷贝出来,所以我们需要自己在磁盘目录新建struts2.3.dtd文件,打开jar包里的struts2.3.dtd文件将其内容拷贝出来保存

    保存完毕后打开eclipse-window-preferencs-xml-xml catalog

     

    点击add

    添加struts-2.3.dtd约束。

    编写struts.xml文件

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="hello" namespace="/hello" extends="struts-default"> <action name="HelloAction" class="com.itheima.web.action.HelloAction" method="hello"> <result name="success">/hello.jsp</result> </action> </package> </struts>

     

    2.5.6 配置核心过滤器

    web.xml文件中添加过滤器

    <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

     

    2.2.7 测试

     

    2.3 Struts2开发流程分析

    从客户端发送请求过来先经过前端控制器(核心过滤器StrutsPrepareAndExecuteFilter)过滤器中执行一组拦截器(一组拦截器就会完成部分功能代码)执行目标Action,在Action中返回一个结果视图,根据Result的配置进行页面的跳转.

    2.4 Struts2的配置详解

    2.4.1 Struts.xml基本配置

    <struts> <!-- package:将Action配置封装.就是可以在Package中配置很多action. name属性: 给包起个名字,起到标识作用.随便起.不能其他包名重复. namespace属性:给action的访问路径中定义一个命名空间 extends属性: 继承一个 指定包 abstract属性:包是否为抽象的; 标识性属性.标识该包不能独立运行.专门被继承 --> <package name="hello" namespace="/hello" extends="struts-default"> <!-- action元素:配置action类 name属性: 决定了Action访问资源名. class属性: action的完整类名 method属性: 指定调用Action中的哪个方法来处理请求 --> <action name="HelloAction" class="com.itheima.web.action.HelloAction" method="hello"> <!-- result元素:结果配置 name属性: 标识结果处理的名称.与action方法的返回值对应. type属性: 指定调用哪一个result类来处理结果,默认使用转发. 标签体:填写页面的相对路径 --> <result name="success">/hello.jsp</result> </action> </package> </struts>

     

    2.4.2 Struts2常量配置

    struts2默认常量配置位置:

    修改struts2常量配置(方式先后也是加载顺序)

    方式1:src/struts.xml

    方式2:在src下创建struts.properties

    方式3:在项目的web.xml中

    加载顺序:

    Struts2常量的具体用法实例

    <!-- 指定Web应用的默认编码集,相当于调用HttpServletRequest的setCharacterEncoding方法 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!- 国际化-把资源文件定为全局变量 baseName为名字--> <constant name="struts.custom.i18n.resources" value="baseName" /> <!--该属性指定需要Struts 2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。 如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开--> <constant name="struts.action.extension" value="do,action,htm,html,jsp" /> <!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开--> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 开发模式下使用,这样可以打印出更详细的错误信息 --> <constant name="struts.devMode" value="true" /> <!-- 默认的视图主题 --> <constant name="struts.ui.theme" value="simple" /> <!-- spring 托管 --> <constant name="struts.objectFactory" value="spring" /> <!--指定加载struts2配置文件管理器,默认为org.apache.struts2.config.DefaultConfiguration 开发者可以自定义配置文件管理器,该类要实现Configuration接口,可以自动加载struts2配置文件--> <constant name="struts.configuration" value="org.apache.struts2.config.DefaultConfiguration" /> <!-- 设置默认的locale和字符编码 --> <constant name="struts.locale" value="zh_CN" /> <constant name="struts.i18n.encoding" value="GBK" /> <!--指定spring框架的装配模式,装配方式有: name, type, auto, and constructor (name是默认装配模式)> <constant name="struts.objectFactory.spring.autoWire" value="name" /> <!-- 该属性指定整合spring时,是否对bean进行缓存,值为true or false,默认为true --> <cosntant name="struts.objectFactory.spring.useClassCache" value="true"/> <!-- 指定类型检查,包含tiger和notiger --> <cosntant name="struts.objectTypeDeterminer" value="tiger" /> <!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 --> <constant name="struts.multipart.parser" value="cos" /> <constant name="struts.multipart.parser" value="pell" /> <constant name="struts.multipart.parser" value="jakarta" /> <!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir --> <constant name="struts.multipart.saveDir" value="/tmpuploadfiles" /> <!-- 该属性指定Struts 2文件上传中整个请求内容允许的最大字节数 --> <constant name="struts.multipart.maxSize" value="2097152" /> <!--该属性指定Struts2应用加载用户自定义的属性文件,该自定义属性文件指定的属性不会覆盖struts.properties文件中指定的属性。如果需要加载多个自定义属性文件,多个自定义属性文 件的文件名以英文逗号(,)隔开。(也就是说不要改写struts.properties!) --> <constant name="struts.custom.properties"value="application,org/apache/struts2/extension/custom" /> <!-- 指定请求url与action映射器,默认为org.apache.struts2.dispatcher.mapper.DefaultActionMapper -> <constant name="struts.mapper.class" value="org.apache.struts2.dispatcher.mapper.DefaultActionMapper" /> <!-- 设置是否支持动态方法调用,true为支持,false不支持. --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- 设置是否可以在action中使用斜线,默认为false不可以,想使用需设置为true. --> <constant name="struts.enable.SlashesInActionNames" value="true" /> <!-- 是否允许使用表达式语法,默认为true. --> <constant name="struts.tag.altSyntax" value="true" /> <!-- 设置当struts.xml文件改动时,是否重新加载 --> <cosntant name="struts.configuration.xml.reload" value="true"

     

    2.5 Action类详解

    2.5.1 Action编写的方式

    Action的是一个POJO的类】

    Action是简单的Java对象没有实现任何借口 和 继承任何类. public class ActionDemo1 { public String execute(){ System.out.println("ActionDemo1执行了..."); return null; } }

     

    Action类实现一个Action的接口】

    public class ActionDemo2 implements Action{ @Override public String execute() throws Exception { System.out.println("ActionDemo2执行了..."); return null; } } Action接口中提供了5个已经定义好的视图名称: * SUCCESS :success,代表成功. * NONE :none,代表页面不跳转 * ERROR :error,代表跳转到错误页面. * INPUT :input,数据校验的时候跳转的路径. * LOGIN :login,用来跳转到登录页面.

     

    Action类继承ActionSupport类】

    public class ActionDemo3 extends ActionSupport{ @Override public String execute() throws Exception { System.out.println("ActionDemo3执行了..."); return NONE; } } ActionSupport中提供了一些功能,比如数据校验,比如国际化… 如果Action继承了ActionSupport,那么Action就会有这些功能.

     

    2.5.2 Action的访问:

    Action的访问不是难题,因为之前已经访问过了,但是出现一个问题一次请求现在对应一个Action,那么如果请求很多对应很多个Action.现在要处理的问题就是要让一个模块的操作提交到一个Action中。

    【解决Action的访问的问题的方式一:通过配置method属性完成】

    页面:

    <h3>客户的管理</h3>

    <a href="${pageContext.request.contextPath }/saveCustomerAction.action">添加客户</a><br/>

    <a href="${pageContext.request.contextPath }/updateCustomerAction.action">修改客户</a><br/>

    <a href="${pageContext.request.contextPath }/deleteCustomerAction.action">删除客户</a><br/>

    <a href="${pageContext.request.contextPath }/findCustomerAction.action">查询客户</a><br/>

     

     

    编写Action:

    public class CustomerActionextends ActionSupport{

     

        public String save(){

            System.out.println("CustomerActionsave方法执行了...");

            return NONE;

        }

        public String update(){

            System.out.println("CustomerActionupdate方法执行了...");

            return NONE;

        }

        public String delete(){

            System.out.println("CustomerActiondelete方法执行了...");

            return NONE;

        }

        public String find(){

            System.out.println("CustomerActionfind方法执行了...");

            return NONE;

        }

    }

     

    配置Action:

        <package name="demo3"extends="struts-default"namespace="/">

            <action name="saveCustomerAction"class="cn.itcast.struts2.demo3.CustomerAction"method="save"></action>

            <action name="updateCustomerAction"class="cn.itcast.struts2.demo3.CustomerAction"method="update"></action>

            <action name="deleteCustomerAction"class="cn.itcast.struts2.demo3.CustomerAction"method="delete"></action>

            <action name="findCustomerAction"class="cn.itcast.struts2.demo3.CustomerAction"method="find"></action>

       

        </package>

     

     

     

    【解决Action的访问的问题的方式二:通过通配符的配置完成】

    第一种解决方案不是很优秀,因为在Action的配置中配置多条.能不能一个Class类只对应一个配置?

     

    页面:

    <h3>联系人的管理</h3>

    <a href="${ pageContext.request.contextPath }/linkman_save.action">添加联系人</a> <br/>

    <a href="${ pageContext.request.contextPath }/linkman_update.action">修改联系人</a> <br/>

    <a href="${ pageContext.request.contextPath }/linkman_delete.action">删除联系人</a> <br/>

    <a href="${ pageContext.request.contextPath }/linkman_find.action">查询联系人</a> <br/>

     

    编写Action:

    public class LinkManActionextends ActionSupport{

     

        public String save(){

            System.out.println("保存联系人...");

            return NONE;

        }

        public String update(){

            System.out.println("修改联系人...");

            return NONE;

        }

        public String delete(){

            System.out.println("删除联系人...");

            return NONE;

        }

        public String find(){

            System.out.println("查询联系人...");

            return NONE;

        }

    }

     

    配置Action:

        <!-- 通配符的配置 -->

        <action name="linkman_*" class="cn.itcast.struts2.demo3.LinkManAction"method="{1}"></action>

     

    【解决Action的访问的问题的方式三:动态方法访问】

    开启一个常量:动态方法访问.

    <constantname="struts.enable.DynamicMethodInvocation"value="true"></constant>

     

    编写Action:

    public class UserAction extends ActionSupport{

     

        public String save(){

            System.out.println("保存用户...");

            return NONE;

        }

        public String update(){

            System.out.println("修改用户...");

            return NONE;

        }

        public String delete(){

            System.out.println("删除用户...");

            return NONE;

        }

        public String find(){

            System.out.println("查询用户...");

            return NONE;

        }

    }

     

    配置Action:

    <!-- 动态方法访问的配置 -->

    <action name="userAction"class="cn.itcast.struts2.demo3.UserAction"></action>

     

    页面路径写法:

    <h3>用户的管理</h3>

    <a href="${pageContext.request.contextPath }/userAction!save.action">添加用户</a><br/>

    <a href="${pageContext.request.contextPath }/userAction!update.action">修改用户</a><br/>

    <a href="${pageContext.request.contextPath }/userAction!delete.action">删除用户</a><br/>

    <a href="${pageContext.request.contextPath }/userAction!find.action">查询用户</a><br/>

     

     

    3.CRM案例

    使用struts2完成客户列表查询

    Struts.xml:

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.configuration.xml.reload" value="true" /> <package name="crm" namespace="/" extends="struts-default"> <action name="CustomerAction_*" class="com.itheima.web.action.CustomerAction" method="{1}"> <result name="list">/jsp/customer/list.jsp</result> </action> </package> </struts>

     

    CustomerAction:

    public class CustomerAction extends ActionSupport { private CustomerService customerService = new CustomerServiceImpl(); /** * 获取客户列表 * @return * @throws Exception */ public String list() throws Exception{ DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); //获取请求参数(明天才学Struts2参数获取 暂时用此方法代替) HttpServletRequest request = ServletActionContext.getRequest(); //获取当期页 int currentPage = 1; //如果参数为空 默认显示第一页 if (request.getParameter("currentPage")!=null && !request.getParameter("currentPage").equals("")) { currentPage = Integer.parseInt(request.getParameter("currentPage")); } //获取筛选客户名称 String cust_name = request.getParameter("cust_name"); if (cust_name!=null) { //如果筛选客户名称不为空添加模糊查询条件 dc.add(Restrictions.like("cust_name", "%"+cust_name+"%")); } //设置每一页显示几条记录 int pageSize = 10; //调用业务层获取客户列表 PageBean<Customer> pb = customerService.getCustomerByPage(dc,currentPage,pageSize); request.setAttribute("pb", pb); return "list"; } }

     

    测试结果:

     

    转载请注明原文地址: https://ju.6miu.com/read-667899.html

    最新回复(0)