You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package cn.itbaizhan.action;
import java.util.Collection;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import cn.itbaizhan.po.User;
import cn.itbaizhan.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
//import com.cds.po.Department;
//import com.cds.po.Role;
//import com.cds.service.DepartmentService;
//import com.cds.service.RoleService;
@SuppressWarnings("serial")
@Component("userAction")
@Scope("prototype")//多例每个请求生成一个新的action
public class UserAction extends ActionSupport {
private User user;
//要调用UserService的方法所以要声明,让spring把其实现类注入。@Resource 默认按 byName 自动注入
// 定义了一个名为UserAction的类用于处理与用户相关的操作
public class UserAction {
// 使用@Resource注解注入名为"userServiceImpl"的UserService实例
@Resource(name="userServiceImpl")
UserService service; // 用户服务实例
// 获取UserService实例的方法
// 获取用户服务实例的方法
public UserService getService() {
// 返回服务实例
return service;
}
}
// 设置UserService实例的方法
// 设置用户服务实例的方法
public void setService(UserService service) {
this.service = service;
}
// 获取User对象的方法
// 获取用户对象的方法
public User getUser() {
// 返回用户对象
return user;
}
public void setUser(User user) {
// 设置用户对象
this.user = user;
}
// 用户操作相关的Action类
public class UserAction extends SomeBaseClass {
@Override
// 执行用户操作的方法,可能包括获取用户名等业务逻辑
public String execute() throws Exception {
// 不要直接访问数据库应该访问业务逻辑层service而service去访问DAOdao去访问数据库
String username= user.getUsername();
// 输出用户名到控制台
System.out.println(username);
}
// 根据用户名查找用户
User user = service.findUserByName(username);
// 如果用户不存在,则保存新用户并返回"success"
class UserAction {
// 保存用户信息的方法如果用户为null则保存用户并返回"success",否则返回"error"
// 如果用户对象为空,则保存当前用户对象并返回成功字符串
if(user==null){
// 调用服务类的save方法保存用户信息
this.service.save(this.user);
// 返回字符串"success",表示操作成功
return "success";
}
// 如果用户存在,则返回"error"
else{
// 返回错误信息
return "error";
}
}
//列表
@SuppressWarnings("unchecked")
// 忽略 unchecked 警告,通常用于泛型代码中
public String listUser(){
// 获取请求对象
Map request = (Map) ActionContext.getContext().get("request");
// 将查询到的所有用户列表放入请求对象中,以便在页面中使用
request.put("listUsers", service.findAllUsers());
// 打印用户列表的大小到控制台
System.out.println("listUsers"+service.findAllUsers().size());
// 返回逻辑视图名称 "listUser"
return "listUser";
}
// 查询用户信息的方法
@SuppressWarnings("unchecked")
// 查询用户信息的方法
public String queryUser(){
// 获取用户名并打印
String username= user.getUsername();
System.out.println(username);
// 获取用户的真实姓名
String name= user.getName();
}
// 查询用户信息并将结果存储在user变量中
User user=this.service.queryUser(username);
// 打印用户信息到控制台
System.out.println("user:"+user);
// 获取request对象以便存储查询结果
Map request = (Map) ActionContext.getContext().get("request");
// 将查询到的用户信息放入request对象中
request.put("queryUser", user);
}
//System