4.Struts2_详解hellostruts
来源:互联网 发布:odn网络 s r什么意思 编辑:程序博客网 时间:2024/06/07 08:25
第四节是在第三节搭配环境后做的小练习。
1.项目结构如下如,包括必须的包 (导入struts2.jar包,导入到lib目录下)
2..web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Struts2-2</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置过滤器类 --> <filter> <filter-name>struts2</filter-name> <!-- 从struts-2.1.3以后,org.apache.struts2.dispatcher.FileDispatcher值被标注为过时,现在是如下写法 --> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- 过滤器用来初始化struts2并处理所有web请求 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- struts基本配置内容 --> <!-- package:包,struts2使用package来组织模块。 package-name:必须,用于区别不同的package;必须是唯一的、可用的变量名;用于其它package来继承; package-namespace:可选,如果它没有给出,则以/为默认值 若namespace有一个非默认值,则要想调用这个包里的Action, 就必选把这个属性所定义的命名空间添加到有关的URL字符串里 例:http:/localhost:8080/contextPath/namespace/actionName.action package-extends:当前包继承哪个包,即可以继承其中的所有的配置。通常情况下继承struts-defaultstruts-default这个包在struts-default.xml文件中定义 --> <package name="struts" namespace="" extends="struts-default"> <!-- 配置一个action:一个struts2的请求就是一个action action-name:用于在一个package里区别不同的action;必须是唯一的、可用的变量名;是调用action时输入路径的组成部分;对应一个struts2的请求的名字(或对一个servletPath,但去除/和扩展名)不包含扩展名 action-class:action所在的路径(包名+类名);默认值为:com.opensymphony.xwork2.ActionSupport,可不写 action-method:action所调用的方法名; 默认执行execute,所以execute可以不写 result:结果 --> <action name="product-input" class="com.opensymphony.xwork2.ActionSupport" method="execute"> <!--result:结果。表示action方法执行后可能返回的一个结果,所以一个action结点可能会有多个result子节点,多个result子节点使用name来区分name:标识一个result和action方法的返回值对应,默认值为success(可不写)type:表示结果的类型。默认值为dispatcher(转发到结果) --> <result name="success" type="dispatcher">/WEB-INF/pages/input.jsp</result> </action> <action name="product-save" class="actionbao.Product" method="save"> <result name="details">/WEB-INF/pages/details.jsp</result> </action> </package> </struts>
4.index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body> <a href="product-input.action">product input</a></body></html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body> <form action="product-save.action" method="post"> ProductName:<input type="text" name="productName"/> <br/> ProductDesc:<input type="text" name="productDesc"/> <br/> ProductPrice:<input type="text" name="productPrice"/> <br/> <input type="submit" value="submit"/> </form></body></html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Insert title here</title></head><body> ProductId:${productId}<br/> ProductName:${productName}<br/> ProductDesc:${productDesc}<br/> ProductPrice:${productPrice}<br/> </body></html>
7.Product.jave
package actionbao;public class Product { private Integer productId; private String productName; private String productDesc; private Double productPrice; public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getProductDesc() { return productDesc; } public void setProductDesc(String productDesc) { this.productDesc = productDesc; } public Double getProductPrice() { return productPrice; } public void setProductPrice(Double productPrice) { this.productPrice = productPrice; } @Override public String toString() { return "Product [productName=" + productName + ", productDesc=" + productDesc + ", productPrice=" + productPrice + "]"; } public String save() { System.out.println("save " + this); return "details"; }}
在Eclipse ee中运行index.jsp即可得出最后结果
思路步骤:
1.由product-input.action转到/WEB-INF/pages/input.jsp
在struts2中配置一个action
<action name="product-input" method="execute">
<result >/WEB-INF/pages/input.jsp</result>
</action>
2.由input.jsp页面的action:product-save.action到Product's save,再到/WEB-INF/pages/details.jsp
在struts2中配置一个action
<action name="product-save" class="actionbao/Product" method="save">
<result name="details"> /WEB-INF/pages/details.jsp</result>
</action>
3.在Product中定义一个save方法,且返回值为details
阅读全文
0 0
- 4.Struts2_详解hellostruts
- struts2_入门实例详解
- Struts2学习——0100HelloStruts
- 4.Struts2_使用XxxAware接口获取web资源
- Struts2_值栈
- Struts2_初见
- Struts2_配置文件
- struts2_入门
- struts2_基础
- Struts2_效验
- Struts2 学习笔记 —— 01 —— HelloStruts
- Struts2_简单配置
- Struts2_校验总结
- Struts2_初识与准备
- Struts2_三种传参数方法
- Struts2_访问Web元素
- Struts2_三种传参数方法
- struts2_方法拦截器
- 欢迎使用CSDN-markdown编辑器
- struts的日期处理(后台Date或者Timestamp)前台显示
- bzoj 2648 (kdtree模板题)
- 总结,报告太麻烦?思维导图帮你忙
- delphi中后缀名为:.dpr,.dfm,.pas,.cfg,.dof,dcu,.res.ddp,分别表示是什么文件?有什么用处?
- 4.Struts2_详解hellostruts
- dubbo+zookeeper报错:com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method
- oracle sqlplus连接
- 模板技术Smarty
- 你最不想错过的 2017 早期 25 个 Android 开源库
- docker-ce的安装,和镜像的配置
- nova中通过FixedIntervalLoopingCall实现的定时任务
- Java Mail发送Email总结(三)--邮件发送的综合例子
- 【转】xapian全文索引库