揭阳网站设计,建设维护网站 未签订合同,网站创建域名,做购物网站能否生存一、下载和搭建Struts2环境
1、 下载地址#xff1a;http://struts.apache.org/#xff0c;该版本为struts-2.3.16.3
2、 搭建Struts2环境
a) 加入jar包#xff1a;复制struts-2.3.16.3\apps\struts2-blank\WEB-INF\lib 下的包到项目WEB-INF\lib 文件夹下#…一、下载和搭建Struts2环境
1、 下载地址http://struts.apache.org/该版本为struts-2.3.16.3
2、 搭建Struts2环境
a) 加入jar包复制struts-2.3.16.3\apps\struts2-blank\WEB-INF\lib 下的包到项目WEB-INF\lib 文件夹下struts2-blank是由strtus2-blank.war 解压出来的文件夹。包文件列表如下图 b) 在 web.xml 中配置 struts2将struts2-blank\WEB-INF\web.xml 文件中关于 filter 的配置复制过来到自己项目的 WEB-INF\web.xml 文件中。完整的web.xml代码如下 ?xmlversion1.0encodingUTF-8? web-appxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlnshttp://java.sun.com/xml/ns/javaeexsi:schemaLocationhttp://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsdidWebApp_IDversion2.5 display-namestruts2Test/display-name welcome-file-list welcome-fileindex.jsp/welcome-file /welcome-file-list !-- 配置struts2的Filter -- filter filter-namestruts2/filter-name filter-classorg.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter/filter-class /filter filter-mapping filter-namestruts2/filter-name url-pattern/*/url-pattern /filter-mapping welcome-file-list welcome-fileindex.html/welcome-file /welcome-file-list /web-app c) 在当前项目的 src 根目录下添加 struts.xml 文件将struts-2.3.16.3\apps\struts2-blank\WEB-INF\classes下的struts.xml 文件复制到src 根目录下然后只保留struts 根节点其他节点内容全部删除。
d) 添加DTD约束Struts.xml 文件中节点自动提示设置
复制http://struts.apache.org/dtds/struts-2.3.dtd这句代码 选择struts-2.3.16.3\src\core\src\main\resources\struts-2.3.dt重新打开struts.xml文件就会出现提示标签。
二、HelloWorld
1、 在 WebContent 目录下创建index.jsp 在index.jsp 中加入超链接 a hrefproduct-input.actionProduct Input/a/html
2、 配置 struts.xml文件 ?xmlversion1.0encodingUTF-8? !DOCTYPEstrutsPUBLIC -//Apache Software Foundation//DTD Struts Configuration 2.3//EN http://struts.apache.org/dtds/struts-2.3.dtd struts !-- 配置struts2可以响应的扩展名 -- constantnamestruts.action.extensionvalueaction,do,/constant !-- struts的请求配置需要放到package下 package:struts2使用package来组织模块 name:包名用于其他包来继承当前包 extends:当前继承哪个包通常继承 struts-default包 -- packagenamehelloWorldextendsstruts-default !-- action:一个struts2的请求就是一个action name:struts请求的名字XXX.action不包含扩展名 result:请求返回的处理结果 -- actionnameproduct-input result/WEB-INF/helloWorld.jsp/result /action /package /struts 3、 helloWorld 页面代码如下 % page languagejavacontentTypetext/html; charsetISO-8859-1 pageEncodingISO-8859-1% !DOCTYPEhtmlPUBLIC-//W3C//DTD HTML 4.01 Transitional//ENhttp://www.w3.org/TR/html4/loose.dtd html head metahttp-equivContent-Typecontenttext/html; charsetISO-8859-1 titleInsert title here/title /head body Hello World! /body /html 三、Action HelloWorld
1、 action VS Action
a) action代表一个struts2 的请求
b) Action类能处理Struts2 请求的类 i. Action类中属性的命名必须遵循与JavaBean属性命名相同的命名规则 ii. Action类必须带有一个无参的构造方法通过反射构建对象 iii. 同一个Action 类可以包含多个action 方法 iv. Struts2 会为每一个HTTP 请求创建一个新的Action实例即Action 不是单例的是线程安全的。
2、 在 WebContent 目录下创建 pages 文件夹在 pages 文件夹中创建input.jsp 和 detail.jsp 文件 Input.jsp 中 body 部分代码如下 body formactioninput.actionmethodpost ProductName:inputtypetextnameproductName/br/br/ ProductDesc:inputtypetextnameproductDesc/br/br/ ProductPrice:inputtypetextnameproductPrice/br/br/ inputtypesubmitnamesubmitvaluesubmit/ /form /body detail.jsp 中 body 部分代码如下 body ProductName:${productName }br/br/ productDesc:${productDesc }br/br/ productPrice:${productPrice } /body
3、 配置 struts.xml 文件 struts packagenamehelloWorldextendsstruts-default actionnameproduct-input result/WEB-INF/pages/input.jsp/result /action actionnameinputclasshelloworld.Productmethodsave resultnamedetail/WEB-INF/pages/detail.jsp/result /action /package /struts /struts 4、 Product 文件代码如下 package helloworld; public class Product { private StringproductID; private StringproductName; private StringproductDesc; private StringproductPrice; public String getProductID() { returnproductID; } public void setProductID(String productID) { this.productID productID; } public String getProductName() { returnproductName; } public void setProductName(String productName) { this.productName productName; } public String getProductDesc() { returnproductDesc; } public void setProductDesc(String productDesc) { this.productDesc productDesc; } public String getProductPrice() { returnproductPrice; } public void setProductPrice(String productPrice) { this.productPrice productPrice; } Override public String toString() { returnProduct [productID productID , productName productName , productDesc productDesc , productPrice productPrice ]; } public String save() { System.out.println(product: this); returndetail; } }