使用实例:
package com.lxitedu.struts2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.lxitedu.bean.Book;
import com.lxitedu.bean.Bookcase;
import com.lxitedu.bean.Type;
import com.lxitedu.homework.ReadFileContainer;
import com.lxitedu.service.BookService;
import com.lxitedu.service.BookcaseService;
import com.lxitedu.service.TypeService;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
private static final long serialVersionUID = 1L;
HttpServletResponse response = ServletActionContext.getResponse();
private Logger logger = Logger.getLogger(BookAction.class);
private static BookService bookService = (BookService) ReadFileContainer
.getContainer().instance("bookService");
private BookcaseService bookcaseService = (BookcaseService) ReadFileContainer
.getContainer().instance("bookcaseService");
private TypeService typeService = (TypeService) ReadFileContainer
.getContainer().instance("typeService");
private Book book = null;
private String names;
public void setBook(Book book) {
this.book = book;
}
public Book getBook() {
return book;
}
public void add() throws IOException {
logger.debug("BookAction add start");
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
bookService.add(book);
response.getWriter().print(true);
logger.debug("BookAction add close");
}
public void get() throws IOException {
logger.debug("BookAction get start");
if (names == null) {
logger.debug("GradeAction get start");
response.setCharacterEncoding("utf-8");
List list = bookService.getAll();
JSONObject json = new JSONObject();
json.put("total", list.size());
json.put("rows", list);
response.getWriter().print(json.toString());
logger.debug("GradeAction get close");
} else {
List lists = new ArrayList();
lists = bookService.getAllBookByNo(Long.parseLong(getNames()));
JSONObject json = new JSONObject();
json.put("total", lists.size());
json.put("rows", lists);
response.getWriter().print(json.toString());
}
logger.debug("BookAction get close");
}
public void delete() {
logger.debug("BookAction delete start");
HttpServletRequest request = ServletActionContext.getRequest();
long id = Long.parseLong(request.getParameter("id"));
bookService.delete(id);
logger.debug("BookAction delete close");
}
public void getTypeList() throws IOException {
logger.debug("BookAction getTypeList start");
List typeList = typeService.getAll();
JSONObject jsonObject = new JSONObject();
jsonObject.put("total", typeList.size());
jsonObject.put("rows", typeList);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().print(jsonObject.toString());
logger.debug("BookAction getTypeList close");
}
public void getBookcaseList() throws IOException {
logger.debug("BookAction getBookcaseList start");
List bookcaseList = bookcaseService.getAll();
JSONObject jsonObject = new JSONObject();
jsonObject.put("total", bookcaseList.size());
jsonObject.put("rows", bookcaseList);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("UTF-8");
response.getWriter().print(jsonObject.toString());
logger.debug("BookAction getBookcaseList close");
}
public void updateBook() throws IOException {
logger.debug("BookAction updateBook start");
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
bookService.update(book);
response.getWriter().print(true);
logger.debug("BookAction updateBook close");
}
public void setNames(String names) {
this.names = names;
}
public String getNames() {
return names;
}
}
文件:log4j.properties
## LOGGERS ##
log4j.rootLogger=debug,console,rolling,daily
log4j.logger.org=error
log4j.logger.net=error
log4j.logger.com.opensymphony.xwork2=error
log4j.logger.org.springframework=error
log4j.appender.daily=org.apache.log4j.DailyRollingFileAppender
log4j.appender.daily.File=D:/log/Daily_log.txt
log4j.appender.daily.DatePattern='.'yyyy-MM-dd
log4j.appender.daily.layout=org.apache.log4j.PatternLayout
log4j.appender.daily.layout.ConversionPattern=%nLEVEL: %p%nTIME: %d%n%l%n%m%n
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%n Level: %p%n Time: %d%n Method: %l%nMessage: %m%n
log4j.logger.com.lxitedu = error //开发的时候设置为debug
log4j.logger.com.ibatis = error
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = error
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = error
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = error
log4j.logger.java.sql.Connection = error
log4j.logger.java.sql.Statement = error
log4j.logger.java.sql.PreparedStatement = error,fileout
log4j.logger.java.sql.ResultSet = error //开发的时候设置为debug
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=D:/log/rolling_log.txt
log4j.appender.rolling.MaxFileSize=100KB
log4j.appender.rolling.MaxBackupIndex=1
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=%nLEVEL: %p%nTIME: %d%n%l%n%m%n
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net