baseAction<T>一例

本文介绍了一个用于Struts2框架的通用Action基类的设计实现,该基类通过泛型支持多种业务模型,并利用反射机制动态创建实例。此外,还包括了与应用上下文、会话及请求相关的属性设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.ssh.demo.action;
02  
03 import java.io.InputStream;
04 import java.lang.reflect.ParameterizedType;
05 import java.util.List;
06 import java.util.Map;
07  
08 import javax.annotation.Resource;
09  
10 import org.apache.struts2.interceptor.ApplicationAware;
11 import org.apache.struts2.interceptor.RequestAware;
12 import org.apache.struts2.interceptor.SessionAware;
13  
14 import com.opensymphony.xwork2.ActionSupport;
15 import com.opensymphony.xwork2.ModelDriven;
16 import com.ssh.demo.service.UserService;
17  
18 @SuppressWarnings("unchecked")
19 public class BaseAction<T> extends ActionSupport implements RequestAware,
20         SessionAware, ApplicationAware, ModelDriven<T> {
21  
22     protected T model; // model 有可能为 user,student,teacher等等........
23     protected Map<String, Object> jsonMap = null;
24     protected List<T> jsonList = null;
25     protected InputStream inputStream = null;
26     protected Map<String, Object> application;
27     protected Map<String, Object> session;
28     protected Map<String, Object> request;
29  
30     @Resource
31     protected UserService userService;
32     /**
33      * 通过反射动态的创建对象
34      */
35     public BaseAction() {
36         ParameterizedType type = (ParameterizedType) this.getClass()
37                 .getGenericSuperclass();
38         Class clazz = (Class) type.getActualTypeArguments()[0];
39         try {
40             model = (T) clazz.newInstance();
41         catch (Exception e) {
42             throw new RuntimeException(e);
43         }
44     }
45  
46     @Override
47     public T getModel() {
48         return model;
49     }
50  
51     public void setJsonMap(Map<String, Object> jsonMap) {
52         this.jsonMap = jsonMap;
53     }
54  
55     public Map<String, Object> getJsonMap() {
56         return jsonMap;
57     }
58  
59     public List<T> getJsonList() {
60         return jsonList;
61     }
62  
63     public void setJsonList(List<T> jsonList) {
64         this.jsonList = jsonList;
65     }
66  
67     public InputStream getInputStream() {
68         return inputStream;
69     }
70  
71     public void setInputStream(InputStream inputStream) {
72         this.inputStream = inputStream;
73     }
74  
75     @Override
76     public void setApplication(Map<String, Object> application) {
77         this.application = application;
78     }
79  
80     @Override
81     public void setSession(Map<String, Object> session) {
82         this.session = session;
83     }
84  
85     @Override
86     public void setRequest(Map<String, Object> request) {
87         this.request = request;
88     }
89 }
使用中文解释ssm管理系统文件action文件里的以下代码package com.action; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.entity.Marks; import com.entity.Programs; import com.entity.Student; import com.entity.Teacher; import com.service.MarksService; import com.service.ProgramsService; import com.service.StudentService; import com.service.TeacherService; import com.util.PageHelper; import com.util.VeDate; //定义为控制器 @Controller // 设置路径 @RequestMapping(value = "/marks", produces = "text/plain;charset=utf-8") public class MarksAction extends BaseAction { // 注入Service 由于标签的存在 所以不需要getter setter @Autowired @Resource private MarksService marksService; @Autowired @Resource private TeacherService teacherService; @Autowired @Resource private StudentService studentService; @Autowired @Resource private ProgramsService programsService; // 准备添加数据 @RequestMapping("createMarks.action") public String createMarks() { List<Student> studentList = this.studentService.getAllStudent(); this.getRequest().setAttribute("studentList", studentList); List<Programs> programsList = this.programsService.getAllPrograms(); this.getRequest().setAttribute("programsList", programsList); return "addmarks"; } // 添加数据 @RequestMapping("addMarks.action") public String addMarks(Marks marks) { double total = 0; List<Programs> programsList = this.programsService.getAllPrograms(); for (Programs p : programsList) { String pnum = this.getRequest().getParameter("num_" + p.getProgramsid()); System.out.println(pnum); total += Double.parseDouble(pnum); } String teacherid = (String) this.getSession().getAttribute("teacherid"); marks.setTeacherid(teacherid); marks.setAddtime(VeDate.getStringDateShort()); marks.setScore("" + VeDate.getDouble(total)); this.marksService.insertMarks(marks); return "redirect:/marks/createMarks.action"; }
05-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值