[原创]java WEB学习笔记54:Struts2学习之路--- 编写Struts2 的第一个程序,HelloWord,简述 package ,action,result

本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用

内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系。

本人互联网技术爱好者,互联网技术发烧友

微博:伊直都在0221

QQ:951226918

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

1.详细介绍原理

  1)关于Struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 
 8         package: 包. Struts2 使用package 来组织模块
 9         name属性:必须. 用于其他的包引用当前包。一般为当前模块的名字
10         extends:当前包继承哪个包,继承的,即可以继承其中的所有的配置。通常情况下,继承 struts-default
11         namespace:可选,如果没有给出,则 以 "/" 为默认值。若namespace有一个非默认值,则要想调用这个包里面的action,则必须把这个属性所定义的命名空间添加到先关的URI 字符串里。在servletPath 前面添加。
12          例如: http://localhost:8080/contextpath/namespace/servletPath
13          
14          
15      -->
16     <package name="helloword" extends="struts-default">
17         
18         <!-- 配置一个action: 一个struts2 的请求就是一个action
19         
20             name:对应一个Struts2 的请求的名字(或者对应一个servletPath,但是去除 / 和 扩展名),不含扩展名
21             class:的默认值为:com.opensymphony.xwork2.ActionSupport
22             method:的默认值为:execute
23             
24          -->
25         <action name="product-input" 
26                 class="com.opensymphony.xwork2.ActionSupport"
27                 method="execute">
28         <!-- 
29                 
30             result:结果。即,要访问的路径。表示action 方法执行后可能返回的一个结果。一个action节点可能有多个result 子节点。多个子节点使用name属性来区分,
31             name:标示一个一个result,和action 方法的返回值对应。默认值为 success
32             type:表示结果的类型,默认为dispatcher(转发到 结果)
33         -->
34             <result name="success" type="dispatcher">/struts2/input.jsp</result>
35         </action>
36         
37         
38         <action name="product-save" class="com.jason.struts.helloword.Product" method="save">
39             <result name="details">/struts2/details.jsp</result>
40 
41         </action>
42     
43     </package>
44     
45 </struts>

   

  ① package

  

② action

③ result

2.代码

代码结构

index.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 
11 
12     <a href="product-input.action">Product Input</a>
13 </body>
14 </html>

input.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>input page</title>
 8 </head>
 9 <body>
10         
11     <form action="product-save.action" method="post">
12         ProductName:<input type="text" name="productName"/>
13         <br><br>
14         
15         ProductDesc:<input type="text" name="productDesc"/>
16         <br><br>
17 
18         ProductPrice:<input type="text"  name="productPrice"/>
19         <br><br>
20 
21         <input type="submit"  value="submit" />
22     </form>
23 </body>
24 </html>

detail.jsp

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>detail page</title>
 8 </head>
 9 <body>
10     
11         ProductId:${productId }
12         <br><br>
13         ProductName:${productName }
14         <br><br>
15         ProductDesc:${productDesc }
16         <br><br>
17         ProductPrice:${productPrice }
18         <br><br>
19         
20         
21 </body>
22 </html>

Product.java

 1 package com.jason.struts.helloword;
 2 
 3 public class Product {
 4     
 5     private Integer productId;
 6     private String productName;
 7     private String productDesc;
 8      
 9     private double productPrice;
10     
11     
12     
13     public String save(){
14         
15         System.out.println(this);
16         return "details";
17     }
18     @Override
19     public String toString() {
20         return "Product [productId=" + productId + ", productName="
21                 + productName + ", productDesc=" + productDesc
22                 + ", productPrice=" + productPrice + "]";
23     }
24 
25     public Integer getProductId() {
26         return productId;
27     }
28 
29     public void setProductId(Integer productId) {
30         this.productId = productId;
31     }
32 
33     public String getProductName() {
34         return productName;
35     }
36 
37     public void setProductName(String productName) {
38         this.productName = productName;
39     }
40 
41     public String getProductDesc() {
42         return productDesc;
43     }
44 
45     public void setProductDesc(String productDesc) {
46         this.productDesc = productDesc;
47     }
48 
49     public double getProductPrice() {
50         return productPrice;
51     }
52 
53     public void setProductPrice(double productPrice) {
54         this.productPrice = productPrice;
55     }
56     
57 
58 
59 }

struts.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 
 6 <struts>
 7     <!-- 
 8         package: 包. Struts2 使用package 来组织模块
 9         name属性:必须. 用于其他的包引用当前包。一般为当前模块的名字
10         extends:当前包继承哪个包,继承的,即可以继承其中的所有的配置。通常情况下,继承 struts-default
11     
12      -->
13     <package name="helloword" extends="struts-default">
14         
15         <!-- 配置一个action: 一个struts2 的请求就是一个action
16         
17             name:对应一个Struts2 的请求的名字(或者对应一个servletPath,但是去除 / 和 扩展名),不含扩展名
18             result:结果。即,要访问的路径
19          -->
20         <action name="product-input" >
21             <result>/struts2/input.jsp</result>
22         </action>
23         
24         
25         <action name="product-save" class="com.jason.struts.helloword.Product" method="save">
26             <result name="details">/struts2/details.jsp</result>
27 
28         </action>
29     
30     
31     
32     </package>
33     
34     
35     
36 </struts>

  1.由 product-input.action 转到/struts2/input.jsp, 在struts.xml 中配置一个acton

  

1         <!-- 配置一个action: 一个struts2 的请求就是一个action
2         
3             name:对应一个Struts2 的请求的名字(或者对应一个servletPath,但是去除 / 和 扩展名),不含扩展名
4             result:结果。即,要访问的路径
5          -->
6         <action name="product-input" >
7             <result>/struts2/input.jsp</result>
8         </action>
9         

2.由input.jsp 页面的action: product-save.action 到 /struts2/details.jsp 页面。在Product 中定义一个save方法,且返回值为 details 

1 <action name="product-save" class="com.jason.struts.helloword.Product" method="save">
2             <result name="details">/struts2/details.jsp</result>
3 
4 </action>
原文地址:https://www.cnblogs.com/jasonHome/p/5729665.html