欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 编程资源 > 编程问答 >内容正文

编程问答

尚硅谷公开课-凯发k8官方网

发布时间:2025/1/21 编程问答 9 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

2019独角兽企业重金招聘python工程师标准>>>

1、在eclipse中新建一个java web项目

2、复制jar包

在下载的struts2中,有一个apps文件夹,这个文件夹下的.war文件即是官方给出的例子,其中struts2-blank.war是一个空的应用,即里面什么都没有。但是这个并不是最小的应该。

解压struts2-blank.war,将struts2-blank\web-inf\lib下的.jar文件复制到java web项目中的lib文件夹中、


3、配置web.xml文件

复制struts2-blank中web.xml中关于fileter的配置代码到java web项目的web.xml中,我的web.xml文件如下:


struts2-2struts2org.apache.struts2.dispatcher.ng.filter.strutsprepareandexecutefilterstruts2/*index.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jsp 这个配置的意思是:


 所有的请求都要被strutsprepareandexecutefilter所拦截

4、添加struts-2的配置文件

将struts2-blank\web-inf\classes目录下的struts2.xml文件复制到src下。可以删除多余的东西,只保留struts根结点


5、添加struts.xml的提示

复制struts2.xml中的:http://struts.apache.org/dtds/struts-2.3.dtd

windows->preferences->xml->xml catalog

点击add,将其复制到key后面的文本框中,key type选择uri,

点击file system,添加struts-2.3.dtd,位于:struts-2.3.16.3\src\core\src\main\resources

如图所示:

ok

将struts.xml重新打开,可见提示:


文件列表


├─src │ │ struts.xml │ │ │ └─com │ └─laolang │ └─domain │ product.java │ └─webcontent│ index.jsp││├─pages│ details.jsp│ input.jsp│ps d:\program\java\tomcat\tomcat7\webapps\guigu\struts2\struts2-2>




说明:

product.java

javabean


index.jsp

凯发k8官方网首页,提供一个到input.jsp的链接


input.jsp

输入


details.jsp

显示

代码:

product.java


package com.laolang.domain;/*** the class product.*/ public class product {/*** instantiates a new product.*/public product() {super();}/*** instantiates a new product.** @param productid* the product id* @param productname* the product name* @param productdesc* the product desc* @param productprice* the product price*/public product(integer productid, string productname, string productdesc,double productprice) {super();this.productid = productid;this.productname = productname;this.productdesc = productdesc;this.productprice = productprice;}/*** save.* input的响应action** @return the string*/public string save() {return "details";}/** (non-javadoc)* * @see java.lang.object#tostring()*/@overridepublic string tostring() {return "product [productid=" productid ", productname=" productname ", productdesc=" productdesc ", productprice=" productprice "]";}/*** gets the product id.** @return the product id*/public integer getproductid() {return productid;}/*** sets the product id.** @param productid* the new product id*/public void setproductid(integer productid) {this.productid = productid;}/*** gets the product name.** @return the product name*/public string getproductname() {return productname;}/*** sets the product name.** @param productname* the new product name*/public void setproductname(string productname) {this.productname = productname;}/*** gets the product desc.** @return the product desc*/public string getproductdesc() {return productdesc;}/*** sets the product desc.** @param productdesc* the new product desc*/public void setproductdesc(string productdesc) {this.productdesc = productdesc;}/*** gets the product price.** @return the product price*/public double getproductprice() {return productprice;}/*** sets the product price.** @param productprice* the new product price*/public void setproductprice(double productprice) {this.productprice = productprice;}/** the product id. */private integer productid;/** the product name. */private string productname;/** the product desc. */private string productdesc;/** the product price. */private double productprice; }



index.jsp


<%@ page language="java" contenttype="text/html; charset=utf-8"pageencoding="utf-8"%> struts2例子



input.jsp


<%@ page language="java" contenttype="text/html; charset=utf-8"pageencoding="utf-8"%> 输入
productid:

productname:

productdesc:

productprice:





details.jsp


<%@ page language="java" contenttype="text/html; charset=utf-8"pageencoding="utf-8"%> 显示 <% request.setcharacterencoding("utf-8");%>productid: ${productid }

productname: ${productname }

productdesc: ${productdesc }

productprice: ${productprice }



struts2.xml


/pages/input.jsp/pages/details.jsp



运行效果:


struts2.xml解释

package: 包. struts2 使用 package 来组织模块.
name 属性: 必须. 用于其它的包应用当前包.
extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
struts-default 这个包在 struts-default.xml 文件中定义.

action: 一个 struts2 的请求就是一个 action
name: 对应一个 struts2 的请求的名字(或对一个 servletpath, 但去除 / 和扩展名), 不包含扩展名
class 的默认值为: com.opensymphony.xwork2.actionsupport
method 的默认值为: execute
result: 结果.

转载于:https://my.oschina.net/iamhere/blog/488899

总结

以上是凯发k8官方网为你收集整理的尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图