<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index" class="com.envch.action.IndexAction">
<result name="success">/index.jsp</result>
</action>
</package>
package com.envch.action;
import java.util.ArrayList;
import java.util.List;
import com.envch.model.News;
import com.envch.model.Product;
import com.envch.model.Ptype;
import com.envch.service.CompanyService;
import com.envch.service.NewsService;
import com.envch.service.ProductService;
import com.envch.util.PageVo;
import com.envch.util.SearchVo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class IndexAction extends ActionSupport {
private NewsService ns = new NewsService();
private ProductService ps = new ProductService();
private CompanyService cs = new CompanyService();
private PageVo pageVo = new PageVo();
private SearchVo sv = new SearchVo();
private News news;
public News getNews() {
return news;
}
public void setNews(News news) {
this.news = news;
}
public PageVo getPageVo() {
return pageVo;
}
public void setPageVo(PageVo pageVo) {
this.pageVo = pageVo;
}
public SearchVo getSv() {
return sv;
}
public void setSv(SearchVo sv) {
this.sv = sv;
}
@Override
//重寫struts的execute
public String execute() throws Exception {
List productList = ps.getProductList(pageVo, sv);
Product product = new Product();
List ptypeList = new ArrayList();
Ptype p = new Ptype();
if(productList != null){
for(int i=0; i<productList.size(); i++){
product = (Product)productList.get(i);
p = ps.getPtype(product.getPtype());
ptypeList.add(p);
}
}
//拿到你須要的List
ActionContext.getContext().put("newsList", ns.getNews(pageVo, sv));
ActionContext.getContext().put("productList", productList);
ActionContext.getContext().put("plist", ptypeList);
ActionContext.getContext().put("companyList", cs.getCompanyList());
return SUCCESS;
}
}
我這有兩個方法·不知道能不能幫你:
1.把主頁直接設置成Action,就像樓上的朋友這樣
2.用在頁面加載時用js請求ajax,而後用js寫入頁面。
3.用js調用dwr(跟2同理).