原来读出数据都是通过点击触发某个Action,然后从数据库里返回list,再在jsp页面上显示。
但是因为现在都做一个主页,希望能一打开主页的时候就可以显示一些数据,这就要求不能触发Action了。
由于从来没做过类似这样的功能,所以犯迷糊了。
敢问哪位大侠做过类似的功能,能指导下,给点思路吗?
如果有实例请发我邮箱:[email protected]
我做的是SSH2的。
但是因为现在都做一个主页,希望能一打开主页的时候就可以显示一些数据,这就要求不能触发Action了。
由于从来没做过类似这样的功能,所以犯迷糊了。
敢问哪位大侠做过类似的功能,能指导下,给点思路吗?
如果有实例请发我邮箱:[email protected]
我做的是SSH2的。
那现在这样我等于我action里的方法要重新写过?然后重定向回主页?
哎我都绕糊涂了。
<welcome-file>struts2</welcome-file>
</welcome-file-list>
把欢迎页面换成struts2!
struts2的配置文件里面加入默认的action:<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
<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>struts.xml
<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>IndexAction.java: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同理).
这个servlet 类里你将需要的数据都取出来呗,访问首页就访问这个servlet,就能达到目的
还可以在servlet里将页面进行静态化
http://blog.csdn.net/jayqean/archive/2010/04/18/5499235.aspx
或者LZ你弄自定义标签呗,主页里放的是自定义的标签,访问标签的时候再去doTag()处理,访问数据库
<body>
<jsp:forward page="showIndex.action"></jsp:forward>
</body
showIndex.action 是首页的action ,执行后 实际跳转到action 对应的视图所以经过两次服务器端的转发,地址栏还是:http://localhost/index.jsp而且如果你的web.xml中的welcome file 中设置 index.jsp 作为欢迎页
那么 http://localhost 就直接访问的是showIndex.action执行后的视图页面.记得web.xml中设置如下<!-- struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>