//先创建一个ServletAction,里面写doget 和dopost方法
//获取请求路径/MVCplay/add.do
String uri = req.getRequestURI();
System.out.println(uri);
//根据uri进行截取获取action请求的键
String path = uri.substring(uri.lastIndexOf("/")+1,uri.indexOf("."));
System.out.println(path);
``//再写一个初始化方法进行配置文件
private Properties ps=new Properties();
@Override
public void init() throws ServletException {
//根据初始化方法获得action配置
try {
ps.load(this.getServletContext().getResourceAsStream("/WEB-INF/config.properties"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//再回到dopost方法进行配置
//根据配置文件获取文件com.hlp.action.AddAction
String fileter=(String) ps.get(path);
System.out.println(fileter);
//将fileter进行分割
String[] split = fileter.split(",");
System.out.println(split[0]+"----------"+split[1]);
try {
//获取action类
Class c=Class.forName(split[0]);
//获取实体类
Class c1=Class.forName(split[1]);
//写一个实体类的帮助类将object强转为实体类帮助类
ActionForm form=(ActionForm) c1.newInstance();
//使用map获取表单中的值
Map<String, String > map=new HashMap<String, String>();
//获取表当中的数据
Enumeration<String> enu = req.getParameterNames();
while (enu.hasMoreElements()) {
//获取表单中的name名
String name = enu.nextElement();
//根据name名获取表单中的值
String value = req.getParameter(name);
//将表单中的数据存入map集合当中
//写一个utils类用来收集表单中的数据
}
BeanUtils.populate(map, form);
//实例化
Object object = c.newInstance();
//将object强转为action实现对方法的调用
Action action=(Action)object;
//重写action里面的方法返回action类的返回方式
ActionForword forword = action.excute(req, resp);
forword.forword(req, resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//跳转方式的帮助类
//定义action路径
private String path;
//定义跳转方式
private boolean redirect=false;
public ActionForword(String path, boolean redirect) {
super();
this.path = path;
this.redirect = redirect;
}
public void forword(HttpServletRequest req,HttpServletResponse resp) {
try {
if (redirect) {
resp.sendRedirect(path);
}
else {
req.getRequestDispatcher(path).forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//action接口
//写一个excute方法
public ActionForword excute(HttpServletRequest req,HttpServletResponse resp);
//收集表单数据的帮助类BeanUtils
//帮助类用于收集数据
//写一个populate方法里面传入一个map集合和一个对象
public static void populate(Map<String, String> map,Object object) {
//获取类
Class class1 = object.getClass();
//遍历map集合
for (String name : map.keySet()) {
String value = map.get(name);
//拼接实体类中的set方法
StringBuffer sb=new StringBuffer("set");
//将name中的第一个字母大写
sb.append(name.substring(0,1).toUpperCase());
sb.append(name.substring(1));
//获取实体类中的属性
Field field;
try {
field = class1.getDeclaredField(name);
//解锁打开访问权限
field.setAccessible(true);
//获取该实体类中的方法
Method method=class1.getDeclaredMethod(sb.toString(), field.getType());
Object object2=null;
if (field.getType().equals("java.util.Date")) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
object2=sdf.parse(value);
}
else {
System.out.println(field.getType().getName());
object2=field.getType().getConstructor(String.class).newInstance(value);
}
method.invoke(object, object2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//每一个action类都要重写action接口当中的方法
MVC框架的构造
最新推荐文章于 2020-01-14 15:38:38 发布