在网站上经常能看见一个一个的小的新闻模块,比如csdn的“版主推荐-技术区”模块,这些模块具有很高的独立性和重用性。这个功能在asp中可能比较好实现,直接有控件可以用,而在jsp中也有至少两个方法。方法一:直接将那部分功能做到一个jsp页面中,然后再需要使用的地方直接include这个页面。方法二:将那个模块做成自定义的jsp标签,通过标签的形式放入的指定的地方。
效果图:
<!-- 这里是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.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>wx_test</shortname>
<uri>http://www.wx.com.cn/wx-ui-test</uri>
<tag>
<name>myLinkPanel</name>
<tagclass>com.cn.wuxiong.ui.uitest.LinkPanel</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>titleLogo</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>width</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>height</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
//---------------------------------------------这里是标签处理类--------------------------------------------------
package com.cn.wuxiong.ui.uitest;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.cn.wuxiong.ui.UiUtils;
public class LinkPanel extends BodyTagSupport {
private String titleLogo;
private String width;
private String height;
private String rowAmount;
private String icon;
private String preFixLink;
private String sufFixLink;
private Collection<?> items;
private String buttonLabel;
private String handleFunc;
public String getTitleLogo() {
return titleLogo;
}
public void setTitleLogo(String titleLogo) {
this.titleLogo = titleLogo;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getRowAmount() {
return rowAmount;
}
public void setRowAmount(String rowAmount) {
this.rowAmount = rowAmount;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getPreFixLink() {
return preFixLink;
}
public void setPreFixLink(String preFixLink) {
this.preFixLink = preFixLink;
}
public String getSufFixLink() {
return sufFixLink;
}
public void setSufFixLink(String sufFixLink) {
this.sufFixLink = sufFixLink;
}
public Collection<?> getItems() {
return items;
}
public void setItems(Collection<?> items) {
if (null != items && items.size() > 0)
this.items = items;
}
public String getButtonLabel() {
return buttonLabel;
}
public void setButtonLabel(String buttonLabel) {
this.buttonLabel = buttonLabel;
}
public String getHandleFunc() {
return handleFunc;
}
public void setHandleFunc(String handleFunc) {
this.handleFunc = handleFunc;
}
public LinkPanel() {
}
private Iterator<?> ite = null;
@Override
public int doStartTag() throws JspException {
//System.out.println("doStartTag^^^^^^^^^^^^^^^^^^^");
ite = this.items.iterator();
StringBuffer tempstr = new StringBuffer();
try {// 输出表格开始内容style="border:1px solid #f990033;">
tempstr.append("<div style=\"border:#707070 solid 1px ; ");
tempstr.append("width: ");
tempstr.append(getWidth()+"; ");
tempstr.append("height: ");
tempstr.append(getHeight() + "; \">\n");
tempstr.append("<table ");
tempstr.append(UiUtils.createKeyValueWithEqualMark("width", "100%"));
tempstr.append("><tbody>");
tempstr.append("<tr>");
tempstr.append("<td colspan=\"3\" align=\"right\">");
if(null != getButtonLabel() && !"".equals(getButtonLabel())){
tempstr.append(UiUtils.createInput("button", getButtonLabel(), "",getHandleFunc()));
}else{
tempstr.append(" ");
}
tempstr.append("</td>");
tempstr.append("</tr>");
if (null != ite) {
while (ite.hasNext()) {
// <td><a href="/csg/csg/expertInfo.do&expertId=001"></a>
// </td>
String aContent = ite.next().toString();
tempstr.append("<tr>");
//========================这部分添加复选框(需要在属性配置中加上这个的name或者Id不?)========================
tempstr.append("<td>");
tempstr.append(UiUtils.createInput("checkbox", aContent, "ck",""));
tempstr.append("</td>");
//========================这部分添加超链接========================
tempstr.append("<td>");
tempstr.append(UiUtils.createHyperlink(getPreFixLink() + "&" + getSufFixLink()+ aContent, aContent));
tempstr.append("</td>");
//========================这部分添加备用数据项========================
tempstr.append("<td>ee</td>");
tempstr.append("</tr>");
//System.out.println("doStartTag############ "+tempstr.toString());
}
}
this.pageContext.getOut().write(tempstr.toString());
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
StringBuffer tempstr = new StringBuffer();
tempstr.append("</tbody></table></div>");
try {
this.pageContext.getOut().write(tempstr.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
}
@Override
public void release() {
this.items = null;
this.ite = null;
super.release();
}
}
///用到的UiUtil///
package com.cn.wuxiong.ui;
public class UiUtils {
public static String createInput(String inputType,String inputValue,String inputName,String onclickFunc){
String res = "";
res += "<input ";
res += createKeyValueWithEqualMark("type", inputType);
res += createKeyValueWithEqualMark("value", inputValue);
res += createKeyValueWithEqualMark("name", inputName);
res += createKeyValueWithEqualMark("onclick", onclickFunc);
res += ">";
return "\n"+res;
}
/**创建超链接标签
* @param hrefStr 超链接
* @param HyperlinkBodyText 超链接文字
* @return
*/
public static String createHyperlink(String hrefStr,String HyperlinkBodyText){
String res = "";
res += "<a ";
res += createKeyValueWithEqualMark("href", hrefStr);
res += ">";
res += HyperlinkBodyText;
res +="</a>";
return "\n"+res+"\n";
}
public static String createKeyValueWithEqualMark(String key,String value){
String res = "";
if(null != value && !"".equals(value)){
res += key+"=\"";
res += value +"\" ";
}
return res;
}
public static String createKeyValueWithColonMark(String key,String value){
String res = "";
if(null != value && !"".equals(value)){
res += key+":";
res += value +" ";
}
return res;
}
}
///jsp页面使用情况
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!--这里引入了jquery库-->
<%@ include file="/pageFrame/CommonJsp/pageSet.jsp"%>
<!--这里引入自定义标签-->
<%@ taglib uri="/WEB-INF/tld/wx-ui-test.tld" prefix="xy_test" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>MyTagTest.jsp</title>
</head>
<body>
<%
List<String> lll = new ArrayList<String>();
lll.add("张三");
lll.add("李四");
lll.add("王五");
lll.add("赵六");
lll.add("葛七");
String pre = basePath+"/csg/csg/expertInfo.do";
%>
<br><br>
<xy_test:myLinkPanel height="200" width="200" items="<%=lll%>" buttonLabel="收藏…" handleFunc="funcc()"
preFixLink="<%=pre%>" sufFixLink="?expertId=">
</xy_test:myLinkPanel>
</body>
</html>
注:这里主要是为了实现效果,实际用的时候肯定需要根据需要修改其中的部分代码。