SSH 笔记

当传入的属性有重复时,Struts2 会报

No result defined for action and result input

错误

  Struts2 取得ServletContext环境  
        //ServletContext servletContext = (ServletContext) ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT);
        ServletContext servletContext=ServletActionContext.getServletContext();
       ActionInvocation invocation=ActionContext.getContext().getActionInvocation();

System.out.println("Action:"+invocation.getAction().getClass().getName());

System.out.println("Struts2 中配置的Action:"+invocation.getProxy().getActionName());

System.out.println("Struts2 中配置的Action namespace :"+invocation.getProxy().getNamespace());

System.out.println("调用的方法:"+invocation.getProxy().getMethod();


                           取得Spring管理的applicationContext 

       ServletContext sc=sce.getServletContext();

ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sc);
PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");


把Hibernate的session交给Spring 去管理,不然会出现找不到session的情况,使用过滤器实现,每次请求来的时候都会实例化一次
   
   
  1. <!-- 解决Hibernate懒加载问题 -->
  2. <filter>
  3. <filter-name>hibernate4Filter</filter-name>
  4. <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
  5. </filter>
  6. <filter-mapping>
  7. <filter-name>hibernate4Filter</filter-name>
  8. <url-pattern>/*</url-pattern>
  9. </filter-mapping>

监听器是在服务器部署的时候进行实例化
    
    
  1. <!-- Spring 容器初始化 -->
  2. <context-param>
  3. <param-name>contextConfigLocation</param-name>
  4. <param-value>classpath:applicationContext*.xml</param-value>
  5. </context-param>
  6. <listener>
  7. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  8. </listener>

自定义监听器代码
     
     
  1. package david.oa.listener;
  2. import java.util.List;
  3. import javax.servlet.ServletContext;
  4. import javax.servlet.ServletContextEvent;
  5. import javax.servlet.ServletContextListener;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.web.context.support.WebApplicationContextUtils;
  8. import david.oa.domain.Privilege;
  9. import david.oa.service.PrivilegeService;
  10. public class InitServletContextListener implements ServletContextListener{
  11. @Override
  12. public void contextDestroyed(ServletContextEvent arg0) {
  13. // TODO Auto-generated method stub
  14. }
  15. @Override
  16. public void contextInitialized(ServletContextEvent sce) {
  17. // 得到Service的实例对象
  18. //确保是初始化时同一个Spring容器
  19. ServletContext sc=sce.getServletContext();
  20. ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(sc);
  21. PrivilegeService privilegeService = (PrivilegeService) ac.getBean("privilegeServiceImpl");
  22. List<Privilege> topPrivilegeList = privilegeService.findTopList();
  23. sc.setAttribute("topPrivilegeList", topPrivilegeList);
  24. System.out.println("-- 已准备好顶级权限的数据 --");
  25. List<String> allPrivilegeUrls = privilegeService.getAllPrivilegeUrls();
  26. sc.setAttribute("allPrivilegeUrls", allPrivilegeUrls);
  27. System.out.println("-- 已准备好所有权限的URL数据 --");
  28. }
  29. }




Hibernate中,当通过session取得的对象,一般属性会自动加载,无论是否设置为懒加载 lazy=“true”
例如 < property name="name"  /> 这个属性会默认 加载

而对于关联属性例如 <many-to one > 或者<set> 这些属性当  lazy=“true”时是不会立刻加载的,只有当调用bean.getXxx()时才会加载
但是要注意调用getXxx()时是否还是同一个Hibernate session

例如User 中有Roles 这个属性,我把它设置为懒加载时
    
    
  1. <set name="roles" table="user_role" lazy="true" >
  2. <key column="userId"></key>
  3. <many-to-many class="Role" column="roleId" order-by="id DESC"></many-to-many>
  4. </set>

然后我通过下面的Action 查询到一个user时 ,把它存到session作用域中,
    
    
  1. public String list() throws Exception{
  2. User user=(User)ActionContext.getContext().getSession().get("user");
  3. if(user ==null){
  4. user =userService.getById(3l);
  5. ActionContext.getContext().getSession().put("user",user);
  6. }
  7. return METHOD;
  8. }
然后在另外一个页面中用EL表达式取出,发现会报懒加载错误。因为此时的对象已经是托管状态。


Struts2 自定义表达式 ,把原来的源码复制出来,新建一个包名和类名一样的类,就可以覆盖原来的类,然后重写 doStartTag() 和doEenTag();

Struts2 拦截器
在struts.xml中
   
   
  1. <interceptors>
  2. <interceptor name="myStack" class="david.oa.intercetor.PrivilegeIntercetor"></interceptor>
  3. <interceptor-stack name="defaultStack">
  4. <interceptor-ref name="defaultStack"/>
  5. <interceptor-ref name="myStack"/>
  6. </interceptor-stack>
  7. </interceptors>

  david.oa.intercetor.PrivilegeIntercetor 要继承 AbstractInterceptor
并重写 intercept方法
    
    
  1. package david.oa.intercetor;
  2. import javax.annotation.Resource;
  3. import javax.servlet.Servlet;
  4. import javax.servlet.ServletContext;
  5. import org.apache.struts2.ServletActionContext;
  6. import org.apache.struts2.StrutsStatics;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.web.context.support.WebApplicationContextUtils;
  9. import com.opensymphony.xwork2.ActionContext;
  10. import com.opensymphony.xwork2.ActionInvocation;
  11. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  12. import david.oa.domain.User;
  13. import david.oa.service.UserService;
  14. import david.oa.service.impl.UserServiceImpl;
  15. public class PrivilegeIntercetor extends AbstractInterceptor {
  16. @Override
  17. public String intercept(ActionInvocation invocation) throws Exception {
  18. User user = (User) ActionContext.getContext().getSession().get("user");
  19. String actionName = invocation.getProxy().getActionName();
  20. String namespace = invocation.getProxy().getNamespace();
  21. String url = actionName;
  22. if (namespace.endsWith("/")) {
  23. url = namespace + actionName;
  24. } else {
  25. url = namespace + "/" + actionName;
  26. }
  27. // 要去掉开头的'/'
  28. if (url.startsWith("/")) {
  29. url = url.substring(1);
  30. }
  31. if (user == null) {
  32. if (url.startsWith("userAction_login")) {
  33. // 正在登陆的路上
  34. return invocation.invoke();
  35. } else {
  36. return "toLogin";
  37. }
  38. }
  39. if (url.startsWith("userAction_login")) {
  40. return "homeIndex";
  41. }
  42. //ServletContext servletContext = (ServletContext) ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT);
  43. ServletContext servletContext=ServletActionContext.getServletContext();
  44. ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(servletContext);
  45. UserService userService=(UserService) ac.getBean("userServiceImpl");
  46. user = userService.getById(user.getId());
  47. if (user.hasPrivilege(url)) {
  48. // 已经登陆,则判断权限
  49. return invocation.invoke();
  50. }
  51. return "noPrivilege";
  52. }
  53. }

hibernate.xml 中      <property name = "format_sql"> true </property>
可以格式化生成的sql语句



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值