SSH+Freemarker整合一(上)

来源:互联网 发布:不明觉厉网络语言 编辑:程序博客网 时间:2024/06/05 20:46
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; 
}
原创粉丝点击