package com.cd.action.user;
// struts找到com.cd.action为命名空间的根,则当前包的命名空间为/user
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;
import com.cd.entity.User;
import com.cd.service.UserService;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
/**
* Struts2 零配置插件convention-plugin就会让Struts自己创建线程安全的Action实例,而不用Spring创建,并且可以接收Spring注入的Service
* Struts2用了注解后,Action则不用Spring管理了
* 如果加了 @Controller不加@Scope,Spring容器在初始化的时候会创建一个单例模式的userAction,可以将日志级别设置为Debug模式查看创建的bean
* Spring创建的这个userAction实际上沿用任何用处,因为struts2的Action不是由Spring管理创建的
* 可以通过在方法内加log.error("Hash Code = " + hashCode());发现每次执行hashCode不一样,说明这时Action是prototype的
* 所以Strut2采用了零配置注解后,Action不是由Spring管理的
*/
// @Scope("singleton")
// 指定singleton仍然是prototype,xml则必须指定为prototype
// @Controller //用了struts2注解就可以不用加@controller和@scope了
@ParentPackage("struts-default")
// @Namespace("/user")
// 验证可以用注解,也可以使用XML配置
// @Validations(requiredStrings = {
// @RequiredStringValidator(fieldName = "username", message = "用户名不能为空"),
// @RequiredStringValidator(fieldName = "age", message = "年龄不能为空") }, regexFields = { @RegexFieldValidator(fieldName = "age", expression = "^\\d{3}$", message = "年龄输入有误") })
public class UserAction {
private Integer userid;
private String username;
private Integer age;
private String sex;
private Logger log = Logger.getLogger(getClass());
private String message;
private List<User> users = new ArrayList<User>(0);
@Autowired()
private UserService userService;
public UserAction() {
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserService getUserService() {
return userService;
}
@Action(value = "add", results = { @Result(name = "success", location = "/user/result.jsp"),
@Result(name = "input", location = "/user/userAdd.jsp") })
// @Validations(requiredStrings = {
// @RequiredStringValidator(fieldName = "username", message = "用户名不能为空"),
// @RequiredStringValidator(fieldName = "age", message = "年龄不能为空") })
// 该方法上面的验证对其它方法也有影响
public String add() {
String msg = "success";
try {
userService.save(this);
message = "添加成功";
} catch (Exception e) {
message = "添加失败";
e.printStackTrace();
}
return msg;
}
@Action(value = "findAll", results = {
@Result(name = "success", location = "/user/userList.jsp"),
@Result(name = "error", location = "/user/result.jsp") }, exceptionMappings = {
@ExceptionMapping(exception = "java.lang.RuntimeException", result = "error"),
@ExceptionMapping(exception = "java.lang.Exception", result = "error") })
public String findAll() {
String msg = "success";
try {
users = userService.findAll();
if (users == null) {
msg = "error";
}
} catch (RuntimeException e) {
msg = "error";
e.printStackTrace();
message = "RuntimeException查询失败,请重试!" + e.getMessage();
} catch (Exception e) {
msg = "error";
message = "Exception查询失败,请重试!" + e.getMessage();
e.printStackTrace();
}
return msg;
}
@Action(value = "deleteUserById", results = {
@Result(name = "success", location = "/user/result.jsp"),
@Result(name = "error", location = "/user/result.jsp") }, exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "error") })
public String deleteUserById() {
String msg = "success";
try {
userService.deleteUserById(this);
message = "删除成功";
} catch (Exception e) {
msg = "error";
message = "删除异常";
e.printStackTrace();
}
return msg;
}
@Action(value = "updateUserById", results = {
@Result(name = "success", location = "/user/result.jsp"),
@Result(name = "error", location = "/user/result.jsp") }, exceptionMappings = { @ExceptionMapping(exception = "java.lang.Exception", result = "error") })
public String updateUserById() {
String msg = "success";
try {
userService.updateUserById(this);
message = "更新成功";
} catch (Exception e) {
msg = "error";
message = "更新异常";
e.printStackTrace();
}
return msg;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
SSH框架整合例子(struts2+spring2+hibernate3)
共46个文件
xml:12个
class:9个
java:8个
5星 · 超过95%的资源 需积分: 12 21 下载量 14 浏览量
2012-04-28
19:02:40
上传
评论 1
收藏 1006KB RAR 举报
温馨提示
ssh 框架技术 案例 struts2+spring2+hibernate3,实际例子完成简单的增删改查
资源推荐
资源详情
资源评论
收起资源包目录
ssh注解整合案例.rar (46个子文件)
ssh
.springBeans 462B
src
com
cd
entity
User.java 2KB
dao
base
GenericDaoImpl.java 5KB
IGenericDao.java 2KB
UserDao.java 353B
action
user
v.xml 354B
UserAction.java 6KB
service
UserService.java 1KB
inter
base
IGenericService.java 1KB
IUserService.java 322B
applicationContext.xml 1KB
struts.xml 471B
log4j.properties 2KB
proxool.xml 1KB
hibernate.cfg.xml 827B
.classpath 1KB
.mystrutsdata 240B
.mymetadata 282B
.myeclipse
.project 2KB
hibernate.reveng.xml 426B
WebRoot
META-INF
MANIFEST.MF 36B
user
userAdd.jsp 1KB
result.jsp 299B
userList.jsp 1KB
userInfo.jsp 801B
index.jsp 457B
WEB-INF
classes
com
cd
entity
User.class 2KB
dao
base
IGenericDao.class 1KB
GenericDaoImpl.class 8KB
GenericDaoImpl$1.class 2KB
UserDao.class 464B
action
user
v.xml 354B
UserAction.class 5KB
service
UserService.class 2KB
inter
base
IGenericService.class 635B
IUserService.class 330B
applicationContext.xml 1KB
struts.xml 471B
log4j.properties 2KB
proxool.xml 1KB
hibernate.cfg.xml 827B
lib
proxool-0.9.1.jar 196KB
mysql-connector-java-5.0.8-bin.jar 528KB
proxool-cglib.jar 327KB
web.xml 2KB
.myhibernatedata 560B
共 46 条
- 1
资源评论
- luckdjc2014-04-18非常不错的资源 对我帮助很大 谢谢 很清晰
- MarsVolcano2012-09-08源码清晰 适合新手 帮助很大
- FmyJinYi2013-12-17适合新手学习
qu12344321
- 粉丝: 2
- 资源: 16
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功