开发笔记

将用户信息保存到Session中

ServletActionContext.getRequest().getSession().setAttribute(Constant.USER, user);

清除session中的保存信息

ServletActionContext.getRequest().getSession().removeAttribute(Constant.USER);

登录过滤器

public class LoginFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    // TODO Auto-generated method stub
    HttpServletResponse rep= (HttpServletResponse) response;
    HttpServletRequest req=  (HttpServletRequest) request;
    String uri=req.getRequestURI();
    //判断当前请求地址是否是登录的请求地址
    if(!uri.contains("sys/login_")){
        //非登录请求
        if(req.getSession().getAttribute(Constant.USER) != null){
            //说明已经登录过,放行
            chain.doFilter(req, rep);
        }else {
            //没有登录过,跳转到登录界面
            rep.sendRedirect(req.getContextPath()+"/sys/login_toLoginUI.action");
        }
    }else {
        //登录请求,直接放行
        chain.doFilter(req, rep);
    }
}
@Override
public void destroy() {}
}

web.xml
    <!-- 登录过滤器,配置在struts2的前面  -->
  <filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>com.dream.core.filter.LoginFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>loginFilter</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

获取spring中的bean

WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(req.getSession().getServletContext());
                PermissionCheck pc=(PermissionCheck) webApplicationContext.getBean("permissionCheck");
                

解决子框架嵌套的问题

if(window != window.parent){
    window.parent.location.reload(true);
} 

struts2 指定日期格式标签

<s:date name="createTime" format="yyyy-MM-dd HH:mm"/>

struts2 if标签,jq替换标签内容

//异步发布信息,信息的id及将要改成的信息状态
  	function doPublic(infoId, state){
  		//1、更新信息状态
  		$.ajax({
  			url:"${basePath}nsfw/info_publicInfo.action",
  			data:{"info.infoId":infoId, "info.state":state},
  			type:"post",
  			success: function(msg){
  				//2、更新状态栏、操作栏的显示值
  				if("更新状态成功" == msg){
  					if(state == 1){//说明信息状态已经被改成 发布,状态栏显示 发布,操作栏显示 停用
  						$("#show_"+infoId).html("发布");
  						$("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',0)">停用</a>');
  					} else {
  						$("#show_"+infoId).html("停用");
  						$("#oper_"+infoId).html('<a href="javascript:doPublic(\''+infoId+'\',1)">发布</a>');
  					}
  				} else {alert("更新信息状态失败!");}
  			},
  			error: function(){
  				alert("更新信息状态失败!");
  			}
  		});
  	}

<td id="show_<s:property value='infoId'/>" align="center"><s:property 	value="state==1?'发布':'停用'"/></td>
<td align="center">
    <span  id="oper_<s:property value='infoId'/>">
        <s:if test="state==1">
            <a href="javascript:doPublic('<s:property value='infoId'/>',0)">停用</a>
        </s:if><s:else>
            <a href="javascript:doPublic('<s:property value='infoId'/>',1)">发布</a>
         </s:else>
    </span>
         <a href="javascript:doEdit('<s:property value='infoId'/>')">编辑</a>
        <a href="javascript:doDelete('<s:property value='infoId'/>')">删除</a>
 </td>

框架整合:在web.xml中注册spring监听器,启动spring容器:

先整合  struts 和spring
然后    spring 和hibernate
最后		struts spring hibernate 

<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>

final关键字修饰一个变量时

final关键字修饰一个变量时,是指引用变量不能变,引用变量所指向的对象中的内容还是可以改变的

写struts2的核心过滤器的时候可以这样写

去引入jar包驱动下依次找到org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>

可以在项目struts总文件中包含 struts子文件

<include file="com/dream/nsfw/complain/conf/complain-struts.xml"></include> 

Spring配置文件

配置 Spring 核心监听器

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
监听器默认会加载 /WEB-INF/applicationContext.xml 配置文件 通常不会放在这个地主所以还需配置路径

如果不是配置在src根目录下,则需要这么写
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/classes/com/dream/nsfw/user/conf/user-spring.xml</param-value>
</context-param>

如果是配置在src根目录下,则需要这么写 
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/classes/user-spring.xml</param-value>
</context-param>
 
的确是简单了不少,但这样写还是麻烦所以有了如下写法
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:user-spring.xml</param-value>
</context-param>
这样看起来爽多了

3/16/2016 项目笔记

onchange事件触发条件需满足:

1、输入框的值发生改变;2 、输入框失去焦点。事件触发发生在2,而不是1的过程中。