ssh框架的要点记录

1. action中将属性传递到jsp页面:(此处有两种方法)

方法一:

ActionContext act=ActionContext.getContext();
act.put("users", users);

jsp页面中获取: ${users.name}

采用OGNL 表达式获取<br/> 
<s:iterator value="#request.employees"> 
    <s:property value="username"/>,<s:property value="password"/>,<s:property value="gender"/><br/> 
</s:iterator> 
<br/>采用JSTL/EL 表达式获取<br/> 
<c:forEach var="emp" items="${employees}" varStatus="st"> 
    ${emp.username},${emp.password},${gender}<br/> 
</c:forEach> 

方法二:

在action中定义private String mes ;再给该变量生成get和set 方法

mes="对不起,您的用户名或密码有误啊!";

jsp页面中获取: ${mes}

扫描二维码关注公众号,回复: 686310 查看本文章

2.清除session:

清除某一个session:session.removeAttribute("session中变量名");

如果要清除所有session:session.invalidate();

3.在jsp页面中获取当前项目名称:
<%=this.getServletContext().getContextPath() %>
通过标签: ${pageContext.request.contextPath}

4.spring可以启用注解 的方式来配置属性:
重新这样配置bean
<bean id="employeeService" class="com.cz.service.imp.EmployeeService"/>
在EmployeeService 的属性sessionFactory中添加一个注解 @Resource
在applicationContext.xml中启用注解
<context:annotation-config/>

5.懒加载 解决:
${loginuser.department.name} 时候,懒加载问题出现。
解决思路:
明确初始化
方法一:在session还没有关闭时,访问一次 xxx.getXxx(),强制访问数据库。或者Hibernate.initialize(Deparment.class);//select预先查询

方法二:在对象映射文件中 取消懒加载  <lazy=”false”/>

上面方法问题是: 不管你在jsp中使不使用部门的名称,它都有向数据库发出select 请求.
方法三:Spring专门提供了opensessioninview 的方法来解决懒加载.
需要在web.xml文件中添加如下配置:
<filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>
该方法可以有效的减少对数据库的查询,缺点是和数据保持的session,时间延长了.

猜你喜欢

转载自chenzheng8975.iteye.com/blog/1606614