天乙社区8.5.0Beta2

一、简介:
天乙社区是一套基于JAVA技术的网络虚拟社区,采用了Hibernate+Spring+Struts的轻量级J2EE框架.
1、全文检索:天乙社区6.0采用Lucene全文检索,并支持完全国际化多语言的全文检索。
2、MVC框架:天乙社区6.0继续了5.x的Struts框架,但经过优化,WEB端更加简洁高效。
3、集群支持:系统可以运行在集群上。
4、功能方面:大大加强了管理功能,用户可以多样化的定制系统的各项信息,包括用户级别、封锁IP、过滤字等等,论坛功能上主要增加了投票帖、上传附件类型多样、帖子中显示用户信息等等功能等。

下载地址: [url]http://www.laoer.com/[/url]

二、功能分析:
1.web.xml:
在web.xml文件中,定义的filter和listener是有顺序的,涉及到对象的初始化顺序以及引用。特别注意的有如下几个配置:
Spring配置文件:
   
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml</param-value>
</context-param>

applicationContext.xml文件中定义的对象包括,数据源、service、dao和task等bean。其中很多task bean都有特定的作用。

验证图片servlet:

<servlet>
<servlet-name>authimg</servlet-name>
<servlet-class>com.laoer.bbscs.web.servlet.AuthImg</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>authimg</servlet-name>
<url-pattern>/authimg</url-pattern>
</servlet-mapping>

authimg类主要生产验证码图片,并对对cookie敏感内容进行加密。

2、cookie
2-1、关于localhost设置cookie的问题
在天乙BBS项目中,如果不配置cookie的domain而是localhost或127.0.0.1时,却向客户端写入cookie,如果浏览器调试成退出时清空cookie,则cookie:主机名@domain文件会自动删除,起不到保存登录状态的作用。原因如下:

说明:cookie IE cookie文件位置,IE->internet选项->常规->浏览历史记录->设置->查看文件,此文件中可以看到保存的cookie信息;
如果想进一步了解cookie,可以参考BLOG [url]http://topmanopensource.iteye.com/blog/409819[/url]
[b]如果浏览器禁用cookie,则cookie的功能就不能使用了。[/b]

在写Cookie时,设置Cookie.Domain为localhost时,Cookie.Domain会立即失效,而在服务器上需要使用域名来限定Cookie.Domain的作用范围,因此需要使用判断来区分:

if (!CheckFormat.isNull(Domain) && HttpContext.Current.Request.Url.Host.IndexOf(Domain) > -1 && CheckFormat.isValidDomain(HttpContext.Current.Request.Url.Host))

进行以下检查:

Domain是否为空值
当前域名中是否包含Domain字符串
当前域名是否为合法域名
由于localhost不符合以上的第3条,因此Cookie.Domain不会被设置。

当在服务器上运行时,用户使用域名访问的话,上诉3条均成立,因此Cookie.Domain可以被设置,并且Cookie能被保存,直到指定的失效时间为止。

3、struts2 自定义标签:
以face标签为例,该标签主要功能是现实用户图片,过程是:根据规则,将存放在硬盘上的用户信息文件中的图片信息取出来,并显示图片。
3.1jsp引用:

<%@taglib uri="/WEB-INF/bbscs.tld" prefix="bbscs"%>
<html>
<body>
......
<bbscs:face value="%{userSession.id}"/>
......
</body>
</html>


3.2定义bbscs.tld文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>2.2.3</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>bbscs</short-name>
<uri>/bbscs</uri>
<display-name>"BBSCS Tags"</display-name>
......
<tag>
<name>face</name>
<tag-class>com.laoer.bbscs.web.taglib.UserFaceTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>

说明:
<name>face</name>:自定义标签名,页面上引用;
<tag-class>com.laoer.bbscs.web.taglib.UserFaceTag</tag-class>:数据标签内容的实现类;
<body-content>empty</body-content>:说明face标签内无内容;
<attribute>:确定标签在jsp上使用时的属性;例如<required>true</required>说明value属性是必须填写的;

3.3 定义UserFaceTag类

public class UserFaceTag extends BbscsComponentTagSupport {

/**
*
*/
private static final long serialVersionUID = -2113974447691755459L;

public UserFaceTag() {
// TODO 自动生成构造函数存根
}

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

@Override
public Component getBean(ValueStack stack, PageContext pageContext) {
// TODO 自动生成方法存根
return new UserFace(stack, pageContext);
}

protected void populateParams() {
super.populateParams();

UserFace tag = (UserFace) component;
tag.setValue(value);
}

}


定义BbscsComponentTagSupport类:

public abstract class BbscsComponentTagSupport extends StrutsBodyTagSupport {

protected Component component;

public abstract Component getBean(ValueStack stack, PageContext pageContext);

public int doEndTag() throws JspException {
component.end(pageContext.getOut(), getBody());
component = null;
return EVAL_PAGE;
}

public int doStartTag() throws JspException {
component = getBean(getStack(), pageContext);
Container container = Dispatcher.getInstance().getContainer();
container.inject(component);

populateParams();
boolean evalBody = component.start(pageContext.getOut());

if (evalBody) {
return component.usesBody() ? EVAL_BODY_BUFFERED : EVAL_BODY_INCLUDE;
} else {
return SKIP_BODY;
}
}

protected void populateParams() {
component.setId(id);
}

public Component getComponent() {
return component;
}
}

说明:doStartTag方法是执行的入口,很显然是在解析开始标签的时候执行,返回值为标签状态,各状态的含义可以在网上查找到。doEndTag含义类似;

定义UserFace类:

public class UserFace extends Component {

private PageContext pageContext;

private String facePicName = "images/defaultFace.gif";

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public UserFace(ValueStack stack, PageContext pageContext) {
super(stack);
this.pageContext = pageContext;
}

public boolean start(Writer writer) {
boolean result = super.start(writer);

if (value == null) {
value = "top";
} else if (altSyntax()) {
if (value.startsWith("%{") && value.endsWith("}")) {
value = value.substring(2, value.length() - 1);
}
}
String userId = "";
Object idObj = this.getStack().findValue(value);
if (idObj != null) {
userId = (String) idObj;
}
StringBuffer sb = new StringBuffer();

if (StringUtils.isBlank(userId)) {
sb.append("<img src=\"");
sb.append(facePicName);
sb.append("\" alt=\"Face\" />");
try {
writer.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
return result;
} else {
if (userId.startsWith(Constant.GUEST_USERID)) { // 游客
sb.append("<img src=\"");
sb.append(facePicName);
sb.append("\" alt=\"Face\" />");

} else { // 正常用户
WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(this.pageContext
.getServletContext());

UserService us = (UserService) wc.getBean("userService");
UserInfoSimple uis = us.getUserInfoSimple(userId);

if (uis.getHavePic() == 1 && !uis.getPicFileName().equals("-")) {
sb.append("<a href=\"");
sb.append(BBSCSUtil.getUserWebPath(uis.getId()));
sb.append(uis.getPicFileName());
sb.append("\" target=\"_blank\">");
sb.append("<img src=\"");
sb.append(BBSCSUtil.getUserWebPath(uis.getId()));
sb.append(uis.getPicFileName());
sb.append(Constant.IMG_SMALL_FILEPREFIX);
sb.append("\" alt=\"Face\" border=\"0\" class=\"pic1\"/>");
sb.append("</a>");
} else {
sb.append("<img src=\"");
sb.append(facePicName);
sb.append("\" alt=\"Face\" />");
}
}
try {
writer.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

return result;
}

}

说明:
writer.write(sb.toString()):使用write方法将html代码输出,即是在jsp上显示的内容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
计算八字神煞需要用到农历和天干地支的知识,可以使用 PHP 的日期和时间函数来实现。 步骤如下: 1. 获取用户输入的阳历生日,使用 PHP 的 date 函数将其转换为农历生日。 2. 根据农历生日,计算出生年、生月、生日的天干地支。 3. 根据天干地支,查找对应的八字神煞。 以下是一个简单的 PHP 代码示例: ```php // 获取用户输入的生日 $birthday = '1990-01-01'; // 将阳历生日转换为农历生日 $chineseBirthday = getChineseBirthday($birthday); // 计算出生年、月、日的天干地支 list($yearTianGan, $yearDiZhi) = getTianGanDiZhi($chineseBirthday['year']); list($monthTianGan, $monthDiZhi) = getTianGanDiZhi($chineseBirthday['month']); list($dayTianGan, $dayDiZhi) = getTianGanDiZhi($chineseBirthday['day']); // 查找八字神煞 $shenSha = getShenSha($yearTianGan, $yearDiZhi, $monthTianGan, $monthDiZhi, $dayTianGan, $dayDiZhi); // 输出结果 echo '您的八字神煞为:' . implode(',', $shenSha); // 获取农历生日 function getChineseBirthday($birthday) { // 使用 PHP 的 DateTime 类将阳历生日转换为农历生日 $dateTime = new DateTime($birthday); $chineseCalendar = new ChineseCalendar($dateTime); $chineseBirthday = [ 'year' => $chineseCalendar->getChineseYear(), 'month' => $chineseCalendar->getChineseMonth(), 'day' => $chineseCalendar->getChineseDay(), ]; return $chineseBirthday; } // 计算天干地支 function getTianGanDiZhi($chineseValue) { // 天干 $tianGan = ['甲', '乙', '丙', '丁', '戊', '己', '庚', '辛', '壬', '癸']; // 地支 $diZhi = ['子', '丑', '寅', '卯', '辰', '巳', '午', '未', '申', '酉', '戌', '亥']; // 计算天干地支 $index = ($chineseValue - 4) % 60; $tianGanIndex = $index % 10; $diZhiIndex = $index % 12; $tianGanValue = $tianGan[$tianGanIndex]; $diZhiValue = $diZhi[$diZhiIndex]; return [$tianGanValue, $diZhiValue]; } // 查找八字神煞 function getShenSha($yearTianGan, $yearDiZhi, $monthTianGan, $monthDiZhi, $dayTianGan, $dayDiZhi) { // 八字神煞表 $shenShaTable = [ '甲子' => ['天乙', '文昌'], '甲戌' => ['天厨', '文曲'], '乙丑' => ['吊客', '天哭'], '乙酉' => ['陀罗', '天虚'], '丙寅' => ['将星', '天月'], '丙申' => ['天巫', '天德'], '丁卯' => ['天才', '天福'], '丁酉' => ['天寿', '天恩'], '戊辰' => ['天贵', '天使'], '戊戌' => ['天荫', '天罡'], '己巳' => ['天福', '天官'], '己亥' => ['天伤', '天蓬'], '庚午' => ['天空', '天任'], '庚子' => ['天后', '天伯'], '辛未' => ['天印', '天威'], '辛酉' => ['天权', '天禄'], '壬申' => ['天德', '天乙'], '壬子' => ['天才', '天英'], '癸未' => ['天寿', '天巫'], '癸酉' => ['天恩', '天贵'], ]; // 查找八字神煞 $shenSha = []; $key = $yearTianGan . $yearDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } $key = $monthTianGan . $monthDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } $key = $dayTianGan . $dayDiZhi; if (isset($shenShaTable[$key])) { $shenSha = array_merge($shenSha, $shenShaTable[$key]); } return $shenSha; } ``` 需要注意的是,以上代码示例中使用了第三方库 `ChineseCalendar` 来实现阳历和农历的转换,使用前需要先安装该库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值