论坛首页 Java企业应用论坛

SSH+Freemarker整合一(上)

浏览 5586 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-18   最后修改:2009-05-20
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;
}
   发表时间:2009-05-18  
代码不格式化一下??!!!!
0 请登录后投票
   发表时间:2009-05-18  
设计在哪呢,我好像没看到,只是整合而已,都是人家定好的,你照搬罢了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics