struts2

web.xml的配置

添加filter

<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

struts.xml 的配置

为action添加一个映射

<package name="store" extends="default" namespace="/store">
<action name="product" class="com.eryiju.webapp.action.store.ProductAction" method="view">
<result name="success" type="velocity"> /WEB-INF/pages/store/product.page</result>
</action>
</package>

<package>表示一系列相关的action
<package>的namespace属性表示这个package处理的url是以/store开头的
<action> 的name属性表示该处理/store开头的product.html结尾的url(即url是/store/product.html)
<action> 的class属性表示该处理该action的java类是com.eryiju.webapp.action.store.ProductAction
<action> 的method属性表示该处理该action的java类的方法是view
<result> 表示经过com.eryiju.webapp.action.store.ProductAction的view方法处理后的返回值对应的渲染页面(type="velocity"表示渲染模板是velocity)

Action 的实现

//定义action ,继承EryijuAction,实现Preparable接口
public class ProductAction extends EryijuAction implements Preparable {
private ProductManager productManager;
private Product product;

public void prepare(){
}

//处理页面逻辑的方法,要求是无参数,并且返回值是String的方法
public String view(){
product = productManager.get(product.getSku());
return SUCCESS;
}

//通过spring依赖注入的方式设置productManager
public void setProductManager(ProductManager productManager) {
this.productManager = productManager;
}

//通过设置getProduct ,struts2 会自动将product设置到request的attribute中(即request.setAttribute("product",product))
public Product getProduct() {
return product;
}

//通过设置setProduct ,struts2 会自动从request.parameter的product.sku封装成product对象
//(即String sku = request.getParameter("product.sku");Prdouct product = new Product();product.setSku(sku);)
public void setProduct(Product product) {
this.product = product;
}
}

常见问题

如何实现文件上传

1. 在struts.xml中添加action

<action name="uploadFile" class="com.eryiju.webapp.action.FileUploadAction">
<interceptor-ref name="fileUploadStack"/>
<result name="input">/WEB-INF/pages/uploadForm.jsp</result>
<result name="success">/WEB-INF/pages/uploadDisplay.jsp</result>
<result name="cancel" type="redirect-action">mainMenu</result>
</action>

标签<interceptor-ref name="fileUploadStack"/>表示添加文件拦截器,该拦截器会自动将form的body中的文件信息封装成File对象

2. 添加uploadForm.jsp

<form action="uploadFile.html" enctype="multipart/form-data" method="post" validate="true" id="uploadForm">
<input type="file" name="file">
<input type="submit" value="upload"/>
</form>

注意如果是要上传文件一定要设定form 的enctype="multipart/form-data"

3. 修改 com.eryiju.webapp.action.FileUploadAction

  1
 public
 class
 FileUploadAction extends
 BaseAction {
2 private static final long serialVersionUID = -9208910183310010569 L;
3
4 // file 对应form表单中的<input type="file" name="file"/>
5 private File file;
6
7 // fileContentType 对应form表单中的<input type="file" name="file"/>中文件的类型
8 private String fileContentType;
9
10 // fileFileName 对应form表单中的<input type="file" name="file"/>中文件的原始名称
11 private String fileFileName;
12
13 private String name;
14
15 /**
16 * Upload the file
17 * @return String with result (cancel, input or sucess)
18 * @throws Exception if something goes wrong
19 */

20 public String upload() throws Exception {
21 if (this .cancel != null ) {
22 return " cancel " ;
23 }
24
25 // the directory to upload to
26 String uploadDir = ServletActionContext.getServletContext().getRealPath(" /resources " ) + " / " + getRequest().getRemoteUser() + " / " ;
27
28 // write the file to the file specified
29 File dirPath = new File(uploadDir);
30
31 if (!dirPath.exists()) {
32 dirPath.mkdirs();
33 }
34
35 // retrieve the file data
36 InputStream stream = new FileInputStream(file);
37
38 // write the file to the file specified
39 OutputStream bos = new FileOutputStream(uploadDir + fileFileName);
40 int bytesRead;
41 byte [] buffer = new byte [8192 ];
42
43 while ((bytesRead = stream.read(buffer, 0 , 8192 )) != -1 ) {
44 bos.write(buffer, 0 , bytesRead);
45 }
46
47 bos.close();
48 stream.close();
49
50 // place the data into the request for retrieval on next page
51 getRequest().setAttribute(" location " , dirPath.getAbsolutePath() + Constants.FILE_SEP + fileFileName);
52
53 String link = getRequest().getContextPath() + " /resources " + " / " + getRequest().getRemoteUser() + " / " ;
54
55 getRequest().setAttribute(" link " , link + fileFileName);
56
57 return SUCCESS;
58 }
59
60 /**
61 * Default method - returns "input"
62 * @return "input"
63 */

64 public String execute() {
65 return INPUT;
66 }
67
68 public void setFile(File file) {
69 this .file = file;
70 }
71
72 public void setFileContentType(String fileContentType) {
73 this .fileContentType = fileContentType;
74 }
75
76 public void setFileFileName(String fileFileName) {
77 this .fileFileName = fileFileName;
78 }
79
80 public void setName(String name) {
81 this .name = name;
82 }
83
84 public String getName() {
85 return name;
86 }
87
88 public File getFile() {
89 return file;
90 }
91
92 public String getFileContentType() {
93 return fileContentType;
94 }
95
96 public String getFileFileName() {
97 return fileFileName;
98 }
99 }

如何将http param 中数据映射到Action中属性

如有一个添加商品的表单如下:

<form name="productForm" action="saveProduct.html" method="post">
商品名称:<input type="text" name="product.name"/>
商品价格:<input type="text" name="product.productPrice.memberPrice"/>
<input type="submit" value="添加商品"/>
</form>

Product类的定义如下:

public class Product {
private String name;
private ProductPrice productPrice;

public void setName(String name){
this.name = name;
}

public void setProductPrice(ProductPrice productPrice){
this.productPrice = productPrice;
}
}
ProductPrice的定义如下:
public class ProductPrice {
private double memberPrice;

public void setName(String name){
this.name = name;
}

public void setMemberPrice(double memberPrice){
this.memberPrice = memberPrice;
}
}
ProductAction如下:
public class ProductPrice {
private Product product;

public void setProduct(String product){
this.product = product;
}
public String saveProduct(){
//do some thing
//在这里form表单中的param已经被转换为product对象({"product.name"=>product.name,"product.productPrice.memberPrice"=>product.product})
return SUCCESS;
}
}

如何与spring整合

在struts.xml中添加

<constant name="struts.objectFactory" value="spring" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值