package com.ddnet.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.rest.DefaultHttpHeaders;
import org.apache.struts2.rest.HttpHeaders;
import com.ddnet.biz.BookBiz;
import com.ddnet.model.Book;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@ResultPath("/WEB-INF/content/rest")
@Results({
@Result(name = "success", type = "redirectAction", params = { "actionName","book" }),
@Result(name = "json", type = "json")
})
public class BookController extends ActionSupport implements
ModelDriven<Object> {
/**
* 默认执行方法
* @return
*/
public HttpHeaders index() {
System.out.println("===Calling index()===");
list = bookBiz.getAll();
return new DefaultHttpHeaders("index").disableCaching();
}
// 处理不带 id 参数的 GET 请求 进入添加对象页面
public String editNew() {
System.out.println("===Calling editNew()===");
//创建一个新图书
model = new Book();
return "editNew";
}
// 处理不带id参数的POST请求, 保存图书
public HttpHeaders create() {
System.out.println("===Calling create()===");
System.out.println("===model.name:"+model.getName()+ "===");
//保存图书
bookBiz.saveOrUpdate(model);
outJson("ok");
System.out.println("===Ending create()===");
return new DefaultHttpHeaders("json").disableCaching();
}
private void outJson(String json) {
HttpServletResponse response = ServletActionContext.getResponse();
try {
PrintWriter out = response.getWriter();
out.write(json);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//处理带id 参数的GET请求,显示指定图书
public HttpHeaders show()
{
System.out.println("id="+ServletActionContext.getRequest().getParameter("id"));
System.out.println("===Calling show()===");
return new DefaultHttpHeaders("show");
}
// 处理带id参数、且指定操作edit资源的GET请求, 进入编辑页面 (book-edit.jsp)
public String edit()
{
System.out.println("===Calling edit()===");
return "edit";
}
// 处理带id参数的PUT请求,修改图书
public String update()
{
System.out.println("===Calling update()===");
System.out.println("===book.name==="+model.getName());
bookBiz.saveOrUpdate(model);
outJson("ok");
addActionMessage("图书编辑成功!");
return "json";
}
//处理带id参数的PUT请求,修改图书
public String modify()
{
System.out.println("===Calling modify()===");
System.out.println("===book.name==="+model.getName());
bookBiz.saveOrUpdate(model);
addActionMessage("图书编辑成功!");
return "success";
}
// 处理带 id 参数,且指定操作 deleteConfirm 资源的方法
// 进入删除页面 (book.jsp)
public String del()
{
System.out.println("===Calling del()===");
bookBiz.remove(Integer.parseInt(sid));
return "success";
}
// 处理带 id 参数的 DELETE 请求
// 删除图书
public String destroy()
{
//bookBiz.remove(id);
//addActionMessage("成功删除 ID 为" + id + "的图书!");
return "success";
}
public String test() {
System.out.println("===Calling test()===");
return "index";
}
/**
*
*/
private static final long serialVersionUID = 1L;
private String sid = "1";
private Book model = new Book();
private List<Book> list;
private String _method;
// 定义业务逻辑组件
private BookBiz bookBiz = new BookBiz();
public void setModel(Book model) {
this.model = model;
}
public Object getModel() {
return (list != null ? list : model);
}
public List<Book> getList() {
return list;
}
public void setList(List<Book> list) {
this.list = list;
}
public String get_method() {
return _method;
}
public void set_method(String _method) {
this._method = _method;
}
public String getSid() {
System.out.println("===Calling getSid()===");
return sid;
}
public void setSid(String sid) {
System.out.println("===Calling setSid()===");
System.out.println("===sid="+sid);
sid = (sid.equals(""))?"1":sid;
this.sid = sid;
if (Integer.parseInt(this.sid) > 0)
{
this.model = bookBiz.get(Integer.parseInt(this.sid));
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页