structs1.3+spring+hibernate+freemarker实现简单功能

9 篇文章 0 订阅

最近公司让在一个超级老的项目上做二次开发,花了一天时间简单的了解了一下大致的操作,记录一下,方便以后复习

 

项目结构如图:

 

 

 

具体操作步骤如下:


一、导入jar包(文尾部有下载链接)

二、web-inf/tld下新建tld文件(mytag.tld)

      Tld文件是专门的标签配置文件,一个简单的标签配置如下所示,其中:

  1.每个tag对应一个自定义标签;

      2.name:tag的名字,不能重复;

      3.Tag-class:这个自定义标签对应的类;

      4.Attribute:自定义标签中的属性(可以有多个属性)

  5.在属性中还能有:name:定义属性的名称。每个标签的是属性名称必须是唯一的。

      6.Required:指定属性是否是必须的或者可选的,如果设置为false为可选。

      7.Rtexprvalue:声明在运行表达式时,标签属性是否有效。

  这里我只讲到了我用到的标签,关于自定义标签的详细用法请看:http://blog.csdn.net/weixin_38894231/article/details/78250615

<?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>
<tag>
		<name>sortTag</name>
		<tag-class>front.web.tag.sort</tag-class>
		<body-content>JSP</body-content>
		<description>分类的测试</description>
		<attribute>
			<name>name</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>pageMax</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>pageIndex</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>frontRow</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
		<attribute>
			<name>field</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
		</attribute>
	</tag>
  <tag>....</tag>
<tag>....</tag>
</taglib>


 

三、创建对应的自定义标签实体类(sort.java)

public class sort  extends TagSupport{
	private String name="sortTag";
	private String frontRow="10";//前多少条记录 top frontrow
	private String field="*";//获取内容,'*'为所有
	

	@Override
	public int doStartTag() throws JspException 
                //langid是我这项目对应的语言版本,userid是我项目中的一个子项目ID,无非就是传到dao层来个where langid=xxx and userid=xxx ,所以没必要看这段
String langid=(String)pageContext.getRequest().getAttribute("common_user_langid");if(langid==null || !langid.matches("\\d+"))return SKIP_BODY;String userid=(String)pageContext.getRequest().getAttribute("common_user_userid");if(userid==null || !userid.matches("\\d+"))return SKIP_BODY; 
                //springUtil.getBean是获取bean的名字,这个工具类代码贴下面
 SortManagerImpl sortManagerImplFront = (SortManagerImpl) SpringUtils.getBean("abc", pageContext.getServletContext());ArrayList sortList = null;try {
//调用service层
 sortList = sortManagerImplFront.selectSort(new Short(langid),new Integer(userid),new Integer(frontRow));} catch (NumberFormatException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}pageContext.setAttribute(name, sortList);return super.doStartTag();}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getFrontRow() {return frontRow;}public void setFrontRow(String frontRow) {this.frontRow = frontRow;}public String getField() {return field;}public void setField(String field) {this.field = field;}
}




SpringUtils.java

这个工具类会调用Bean工厂ApplicationContext.xml,从实现注入

PS:公司这项目挺老了,用的spring好像不支持注解注入,只能自己到XML里配置

public static BeanFactory getBeanFactory() {
		String[] Springxml = { "applicationContext.xml",
				"applicationContext-lxj.xml", "applicationContext-actions.xml",
				"applicationContext-common.xml",
				"applicationContext-beans.xml", "applicationContext-util.xml","applicationContext-lw.xml","applicationContext-utils-lw.xml","applicationContext-ywm.xml","applicationContext-fhl.xml","applicationContext-lm.xml" };
		BeanFactory factory = new ClassPathXmlApplicationContext(Springxml);
		return factory;
	}

	public static Object getBean(String beanName, ServletContext sc) {
		ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
		return ctx.getBean(beanName);
	}
}


 

/项目名/src/applicationContext-lxj.xml

在这个里配置上这两段话,第一句是配置service层的bean,第二条是配置dao层的bean

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >

      <bean id="abc" class="front.service.SortManagerImpl" autowire="byName" />
      <bean id="sortManagerImplFront" class="front.dao.SortDaoImpl" autowire="byName" />

</beans>


 

service层接口:

package front.service;

import java.util.ArrayList;
import java.util.HashMap;

public interface SortManager {
	ArrayList<HashMap> selectSort(Short langid, Integer userid,Integer frontRow) throws Exception;
}


 

service层实现类:

package front.service;

import java.util.ArrayList;
import java.util.HashMap;
import front.dao.SortDaoImpl;

public class SortManagerImpl implements SortManager{
	private SortDaoImpl sortManagerImplFront;
	
	public void setSortManagerImplFront(SortDaoImpl sortManagerImplFront) {
		this.sortManagerImplFront = sortManagerImplFront;
	}

	@Override
	public ArrayList<HashMap> selectSort(Short langid, Integer userid,Integer frontRow) throws Exception {
		
		StringBuffer sql = new StringBuffer();
		StringBuffer where = new StringBuffer();

		where.append("lang_id=").append(langid).append(" and user_id=").append(userid);


		sql.append("select *").append(" from product_type")
			.append(" where ").append(where).append(" limit "+frontRow);
	
		System.out.println("sql:="+sql.toString());
		return sortManagerImplFront.selectSort(sql.toString());
	}

}



dao层接口:

package front.dao;

import java.util.ArrayList;
import java.util.HashMap;
import com.shoponline.dao.Dao;

public interface SortDao extends Dao {
	ArrayList<HashMap> selectSort (String sql) throws Exception;
}



dao层实现类:

package front.dao;

import java.util.ArrayList;
import java.util.HashMap;
import com.shoponline.daoimpl.BaseDaoJdbcImpl;

public class SortDaoImpl extends BaseDaoJdbcImpl implements SortDao {
	@Override
	public ArrayList<HashMap> selectSort(String sql) throws Exception {
		return listDatabase(sql);
	}
}


 

前台html页面代码:

第一句是引入自定义标签并定义属性,后面是freenarker插件实现的,if这个是判断是否为空。list是遍历循环,相当于java中的for

<@mytag.sortTag name="selectSort" frontRow="5" />
	<#if selectSort ??>
		<#list selectSort as sort>
			<a href="javascript:;" οnclick="keywordSearch('${sort.product_type_name}')">${sort.product_type_name}</a>  
		</#list>
	</#if>



最后实现功能如下:




贴一下jar包:

http://download.csdn.net/download/weixin_38894231/10026714

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值