📄 productplugin.java
字号:
package com.pandahome.struts.plugin;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import com.pandahome.struts.domain.Product;
public class ProductPlugin implements PlugIn {
public void destroy() {
// TODO Auto-generated method stub
}
public void init(ActionServlet arg0, ModuleConfig arg1)
throws ServletException {
// TODO Auto-generated method stub
List<Product> products = new ArrayList<Product>();
ServletContext context = arg0.getServletContext();
String fileName = context.getInitParameter("catalogfile");
String filePath = context.getRealPath(fileName);
try {
InputStream is = new FileInputStream(filePath);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String str = br.readLine();
while (str != null) {
Product p = new Product();
String[] strs = str.split("\\|");
p.setProductCode(strs[0]);
p.setPrice(strs[1]);
p.setQuantity(Integer.parseInt(strs[2]));
p.setDescription(strs[3]);
products.add(p);
str = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(products.size());
context.setAttribute("products", products);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -