SSH+Freemarker整合一(上)

SSH+freemarker: 
1、首先,要导入jar 包:(见附件)  
2、修改web.xml文件:  
<listener> 
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value>classpath:applicationContext.xml</param-value> 
</context-param> 
  
  <filter> 
  <filter-name>FilterDispatcher</filter-name> 
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
  </filter> 
  <filter-mapping> 
  <filter-name>FilterDispatcher</filter-name> 
  <url-pattern>/*</url-pattern> 
  </filter-mapping> 
  
  <!--在结合Freemarker模板渲染使用标签的时候,还需要在“web.xml”文件中配置一个servlet,这样Freemarker才能得到渲染-->
<servlet> 
<servlet-name>JspSupportServlet</servlet-name> 
<servlet-class>org.apache.struts2.views.JspSupportServlet</servlet-class> 
<load-on-startup>1</load-on-startup> 

</servlet> 
<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
  </welcome-file-list> 

3、修改strus.xml  
<package name="hibernate" namespace="/hibernate" extends="struts-default"> 
<action name="personList" class="com.apache.mytest2.struts.PersonAction"> 
<result name="success" type="freemarker">/WEB-INF/html/index.html</result> 
<result name="input">/error.jsp</result> 
</action> 
</package> 

4、在src下freemarker.perproties(可选择加入)  
template_update_delay=0 
datetime_format=yyyy-MM-dd HH:mm:ss 
date_format=yyyy-MM-dd 
time_format=HH:mm:ss 
number_format=0.######; 
boolean_format=true,false 
auto_import="/WEB-INF/ftl/template/include.ftl" as my 
whitespace_stripping=true 
default_encoding=GBK 
tag_syntax=auto_detect 
url_escaping_charset=UTF-8 

5、创建personAction  
public class PersonAction extends ActionSupport { 
private PersonDao personDao; 
private Date nowdate; 

public String execute(){ 
List list = this.personDao.listPerson(); 
ServletActionContext.getRequest().setAttribute("list", list); 
return SUCCESS; 


@Override 
public void validate() { 
// TODO Auto-generated method stub 
super.validate(); 


public void setPersonDao(PersonDao personDao) { 
this.personDao = personDao; 


public Date getNowdate() { 
nowdate=new Date(); 
return nowdate; 


public void setNowdate(Date nowdate) { 
this.nowdate = nowdate; 

6、创建DAO实现类:  
public class PersonDaoImpl extends HibernateDaoSupport implements PersonDao { 

public Person getPerson(long id) { 
// TODO Auto-generated method stub 

Object object = this.getSession().createQuery("from Person where id=?").setParameter(0, id).uniqueResult(); 

return (Person)object; 



public List listPerson() { 
// TODO Auto-generated method stub 
List list = this.getSession().createQuery("from Person").list(); 
return list; 


public void savePerson(Person person) { 
// TODO Auto-generated method stub 
this.getSession().saveOrUpdate(person); 

7、创建实体类:  
public class Person { 

private Long id; 
private String name; 
private Date birthday; 

public Long getId() { 
return id; 

public void setId(Long id) { 
this.id = id; 

public String getName() { 
return name; 

public void setName(String name) { 
this.name = name; 

public Date getBirthday() { 
return birthday; 

public void setBirthday(Date birthday) { 
this.birthday = birthday; 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值