SSH2,怎樣才能不觸發Action而直接讀出數據

你在web.xml裏配置  java

<welcome-file-list>
    <welcome-file>struts2</welcome-file>
  </welcome-file-list>
把歡迎頁面換成struts2!
struts2的配置文件裏面加入默認的action:
web

<default-action-ref name="index" />
        <action name="index" class="com.action.IndexAction">
            <result name="success">/index.jsp</result>
        </action>
這下再寫個IndexAction(繼承與Struts2的ActionSupport),重寫Struts2的execute()方法,方法裏面把須要的list拿到放在容器中!
當每次訪問網站的時候,web.xml就會交給welcome-file處理,若是沒輸入任何action名字就會交給IndexAction去處理!
web.xml
ajax



<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
          </filter>


    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


<welcome-file-list>
    <welcome-file>struts2</welcome-file>
 </welcome-file-list>
apache



<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同理).
相關文章
相關標籤/搜索