struts2+hibernate+spring 中获得request对象 我是想在查询记录时记录日志,在Log类获取ip和日志文件路径但是使用ServletActionContext会出异常,请问改如何解决 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ServletActionContext.getServletContext()这样能取到servletContext? ServletActionContext.getRequest(); 获取方法没错,会不会是别的原因? 应该不会,因为如果我把String path = ServletActionContext.getServletContext().getRealPath("log") + File.separator + "xzy" + ".log"; String ip = ServletActionContext.getRequest().getRemoteAddr();这两个改成String path ="d:\\xxx.log";String ip = "127.0.0.1";就没有问题这里需要继承什么吗? log4j.propertieslog4j.rootLogger =DEBUG,logFilelog4j.appender.logFile =org.apache.log4j.RollingFileAppenderlog4j.appender.logFile.File=f:/log/log.loglog4j.appender.logFile.MaxFileSize=5120KBlog4j.appender.logFile.MaxBackupIndex=10 log4j.appender.logFile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logFile.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%njava code: @SuppressWarnings("unchecked") public String execute() throws Exception { if (logger.isDebugEnabled()) { logger.debug("execute() - start"); } user = userService.getSingleUser(userName); if (password.equals(user.getPassword())) { ActionContext.getContext().getSession().put("user", user); if (logger.isDebugEnabled()) { logger.debug("execute() - end"); } return SUCCESS; } else { if (logger.isDebugEnabled()) { logger.debug("execute() - end"); } return ERROR; } } 加入log4j的jar包在src下建一个log4j.properties文件具体内容在上面在需要加log的地方用logger.debug("execute() - start");logger.debug("execute() - end");括起来运行程序 到路径下的log文件就可以看log了如:log4j.appender.logFile.File=f:/log/log.log楼主可以修改成自己的路径 呵呵,我说错了!能帮我看下我的日志代码怎么错了吗?不是加log4j 实现ServletContextAware这个接口呢? String ip = ServletActionContext.getRequest().getRemoteAddr(); //NullPointException这里换成ServletActionContext.get("request");就ok了 实现ServletContextAware也不行ServletActionContext.get("request");没有这个方法? 我给你敲段原文“ActionContext类没有提供类似的getRequest()方法来获取封装了HttpServletRequest的Map对象,要得到请求Map对象,需要为get()方法传递“request” ” 在MyEclipse就显示错误了Cannot make a static reference to the non-static method get(Object) from the type ActionContext 晕 原来你用的是ServletActionContext这个是与Servlet耦合的我再看看 啊 是取不到IP 我上网看了一圈 说有的防火墙导致getRemoteAddr()不能正常取得IP楼主把防火墙关掉试试 啊 是取不到IP 我上网看了一圈 说有的防火墙导致getRemoteAddr()不能正常取得IP楼主把防火墙关掉试试或者你用 .getRemoteHost() 方法取一下试试 谢谢各位!应该是我对ssh了解不多的原因自己换种方式吧,多学习。 Integer自动拆箱问题 如何分组显示数据 大批量 并发测试 关于js中方法之间的互调 PO 后查询问题 EL调用javabean的方法.急!!! 用AXIS传对象,为什么对象中的Vector总是空的 世界上最悲剧的事,缺少类文件,却找不到jar包 hibernate+struts的问题,在main方法测试没错,改成servlet时说hbm.xml文件找不到。 ejb中如何传送大对象?? hibernate映射问题 struts2的文件下载?
这样能取到servletContext?
String path = ServletActionContext.getServletContext().getRealPath("log") + File.separator + "xzy" + ".log";
String ip = ServletActionContext.getRequest().getRemoteAddr();
这两个改成
String path ="d:\\xxx.log";
String ip = "127.0.0.1";
就没有问题
这里需要继承什么吗?
log4j.rootLogger =DEBUG,logFile
log4j.appender.logFile =org.apache.log4j.RollingFileAppender
log4j.appender.logFile.File=f:/log/log.log
log4j.appender.logFile.MaxFileSize=5120KB
log4j.appender.logFile.MaxBackupIndex=10
log4j.appender.logFile.layout=org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
java code: @SuppressWarnings("unchecked")
public String execute() throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("execute() - start");
} user = userService.getSingleUser(userName);
if (password.equals(user.getPassword())) {
ActionContext.getContext().getSession().put("user", user);
if (logger.isDebugEnabled()) {
logger.debug("execute() - end");
}
return SUCCESS; } else {
if (logger.isDebugEnabled()) {
logger.debug("execute() - end");
}
return ERROR;
} }
logger.debug("execute() - start");
logger.debug("execute() - end");
括起来运行程序 到路径下的log文件就可以看log了如:
log4j.appender.logFile.File=f:/log/log.log楼主可以修改成自己的路径
能帮我看下我的日志代码怎么错了吗?
不是加log4j
ServletActionContext.get("request");
没有这个方法?
的Map对象,要得到请求Map对象,需要为get()方法传递“request” ”
Cannot make a static reference to the non-static method get(Object) from the
type ActionContext
应该是我对ssh了解不多的原因
自己换种方式吧,多学习。