Created
September 24, 2012 14:42
-
-
Save MaySnow/3776278 to your computer and use it in GitHub Desktop.
基于注解的struts2 convention
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.action; | |
import java.util.Map; | |
import org.apache.struts2.convention.annotation.Action; | |
import org.apache.struts2.convention.annotation.Result; | |
import org.apache.struts2.interceptor.SessionAware; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import com.kaishengit.pojo.User; | |
import com.kaishengit.service.UserService; | |
public class AppAction implements SessionAware{ | |
private UserService us; | |
@Autowired | |
public void setUs(UserService us) { | |
this.us = us; | |
} | |
private User user; | |
private String code; | |
private Map<String, Object> session; | |
@Action("index")//对应的jsp为index-success.jsp | |
public String execute(){ | |
return "success"; | |
} | |
@Action(value="login", results={ | |
@Result(name="login",type="redirectAction",params={"actionName","index","code","1001"}), | |
@Result(name="success",type="redirectAction",params={"actionName","list","namespace","/todo"}) | |
}) | |
public String login() { | |
User u = us.login(user); | |
if(u != null) { | |
session.put("cur_user", u); | |
return "success"; | |
} else { | |
code = "1001"; | |
return "login"; | |
} | |
} | |
//get set(页面传值) | |
public User getUser() { | |
return user; | |
} | |
public void setUser(User user) { | |
this.user = user; | |
} | |
public String getCode() { | |
return code; | |
} | |
public void setCode(String code) { | |
this.code = code; | |
} | |
public void setSession(Map<String, Object> session) { | |
this.session = session; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE struts PUBLIC | |
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" | |
"http://struts.apache.org/dtds/struts-2.3.dtd"> | |
<struts> | |
<!-- 把value改成myPackage即与下面package的name相同,这样在下面的package可以定义global-results等 --> | |
<constant name="struts.convention.default.parent.package" value="myPackage"/> | |
<!-- 将默认的content修改为views文件夹 --> | |
<constant name="struts.convention.result.path" value="/WEB-INF/views/"/> | |
<package name="myPackage" extends="convention-default" > | |
</package> | |
</struts> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.kaishengit.action; | |
import java.util.List; | |
import java.util.Map; | |
import org.apache.struts2.convention.annotation.Action; | |
import org.apache.struts2.convention.annotation.Namespace; | |
import org.apache.struts2.convention.annotation.Result; | |
import org.apache.struts2.interceptor.SessionAware; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import com.kaishengit.pojo.Todo; | |
import com.kaishengit.pojo.User; | |
import com.kaishengit.service.TodoService; | |
@Namespace("/todo") | |
public class TodoAction implements SessionAware{ | |
private Map<String, Object> session; | |
private List<Todo> todoList; | |
private String content; | |
private int id; | |
private Todo todo; | |
//依赖注入 | |
private TodoService ts; | |
@Autowired | |
public void setTs(TodoService ts) { | |
this.ts = ts; | |
} | |
@Action("list")//对应的jsp为todo文件夹下的list-success.jsp | |
public String execute(){ | |
User user = (User) session.get("cur_user"); | |
todoList = ts.list(user.getId()); | |
return "success"; | |
} | |
/** | |
* 删除 | |
* @return | |
*/ | |
@Action(value="del",results={ | |
@Result(name="success",type="redirectAction",params={"actionName","list"}) | |
}) | |
public String del(){ | |
ts.del(id); | |
return "success"; | |
} | |
/** | |
* 修改 | |
* @return | |
*/ | |
@Action(value="update",results={ | |
@Result(name="success",type="redirectAction",params={"actionName","list"}) | |
}) | |
public String update(){ | |
ts.update(todo); | |
return "success"; | |
} | |
/** | |
* 去编辑页 | |
* @return | |
*/ | |
@Action("edit") | |
public String toEdit(){ | |
todo = ts.findById(id); | |
return "success"; | |
} | |
/** | |
* 完成计划 | |
* @return | |
*/ | |
@Action(value="success",results={ | |
@Result(name="success",type="redirectAction",params={"actionName","list"}) | |
}) | |
public String doPlan(){ | |
ts.doPlan(id); | |
return "success"; | |
} | |
/** | |
* 去添加新计划页 | |
* @return | |
*/ | |
@Action("new") | |
public String toNew(){ | |
return "success"; | |
} | |
/** | |
* 保存新计划 | |
* @return | |
*/ | |
@Action(value="save",results={ | |
@Result(name="success",type="redirectAction",params={"actionName","list"}) | |
}) | |
public String save() { | |
User user = (User) session.get("cur_user"); | |
ts.save(content,user); | |
return "success"; | |
} | |
//get set | |
public void setSession(Map<String, Object> session) { | |
this.session = session; | |
} | |
public List<Todo> getTodoList() { | |
return todoList; | |
} | |
public void setTodoList(List<Todo> todoList) { | |
this.todoList = todoList; | |
} | |
public String getContent() { | |
return content; | |
} | |
public void setContent(String content) { | |
this.content = content; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public Todo getTodo() { | |
return todo; | |
} | |
public void setTodo(Todo todo) { | |
this.todo = todo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment