日期:2014-05-16 浏览次数:20450 次
先看web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>*.action</url-pattern>
</filter-mapping>
<jsp-config>
<taglib>
<taglib-uri>/tags/lpm</taglib-uri> //跟上面定义的uri值一样
<taglib-location>/WEB-INF/lpm.tld</taglib-location> //写文件所在的路径
</taglib>
</jsp-config>
</web-app>
?在看/WEB-INF/lpm.tld文件代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.2</tlibversion>
<jspversion>1.1</jspversion>
<shortname>lpm</shortname> <!-- 使用标签时的前缀 -->
<uri>/tags/lpm</uri>
<tag>
<name>mytaglib</name> <!-- 使用时跟在前缀后的名字,组合成一个标签 -->
<tagclass>com.china.page.taglibpage</tagclass> <!-- 在这里写刚才定义好的类 -->
<bodycontent>jsp</bodycontent> <!--可以为empty和jsp,如果为empty,开始标签和结束标签之间如果有jsp内容就会出错 -->
<attribute>
<name>formid</name> <!--定义此标签可以用的属性名-->
<required>true</required> <!-- 表示这个属性是否可以为空,为true一定要有属性值 -->
<rtexprvalue>true</rtexprvalue> <!-- 表示是否可以使用JSP表达式 -->
</attribute>
</tag>
</taglib>
?
?
首页index.jsp加一个跳转:
<a href="lpm/step.action">aaa</a>
?action代码:
package com.china.action;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import com.china.model.persion;
import com.china.page.Pageinfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
@Namespace("/lpm")
public class Lpmcon extends ActionSupport{
@Action(value="step",
results={@Result(name="Today",location="/test/abc.jsp")})
public String getRes(){
HttpServletRequest request=ServletActionContext.getRequest();
try{
EntityManagerFactory ef=Persistence.createEntityManagerFactory("mysql");
EntityManager em=ef.createEntityManager();
Query qu=em.createQuery("from persion order by userid");
int count=qu.getResultList().size();
Pageinfo pageinfo=new Pageinfo();
pageinfo.setCount(count);
String temp=request.getParameter("curpage");
if(temp!=null){
pageinfo.setCurpage(Integer.parseInt(temp));
}
List list=qu.setFirstResult((pageinfo.getCurpage()-1)*pageinfo.getPagesize())
.setMaxResults(pageinfo.getPagesize())
.getResultList();
re