要自主开发struts1框架,主要用到 servlet ,反射,等技术。
需要用到以下核心类。
ActionServlet类:继承HttpServlet,核心控制器
Action类:封装Action属性
ActionFather类:抽象类,包含抽象方法excute。
InitResource类:加载类,解析xml配置。
下面就以一个用户添加来分析。
当服务器启动InitResource自动加载解析mystruts.xml,分别把form与action存入两组HashMap中。
并向外界提供两个静态方法
getTypeByName:通过name找出对应类。
getActionByPath:通过Path路径找出对应Action。
我们现在有一个Userinfo的实体类。分别有:name,pass,sex这几个字段。
那么在jsp页面中应该
<form action="userinfo.do?flag=save" method="post">
<table>
<tr>
<th colspan ="2">
用户注册
</th>
</tr>
<tr>
<td>
用户名
</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td>
密码
</td>
<td>
<input type="text" name="pass">
</td>
</tr>
<tr>
<td>
性别
</td>
<td>
<input type="text" name="sex">
</td>
</tr>
<tr>
<td colspan="2" align="Center"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
在web.xml中的配置
<servlet>
<servlet-name>ActionServlet</servlet-name>
<servlet-class>com.mystruts.common.ActionServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ActionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
mystruts.xml中的配置
<?xml version="1.0" encoding="UTF-8"?>
<xml-body>
<form-beans>
<form-bean name="userinfoForm" type="com. mystruts..mystruts.form.UserinfoForm"/>
</form-beans>
<actions>
<action
name="userinfoForm"
type="com. mystruts..mystruts.action.UserinfoAction"
path="/userinfo"
parameter="flag"/>
</actions>
</xml-body>
当点击提交按钮,程序进入ActionServlet的service方法。
第一步:分析路径。通过request的req.getServletPath()方法获取路径,并拆分字符串为/userinfo。
通过InitResource.getActionByPath方法找到对应Action。
第二步:生成Action实例。
第三步:调用Action中的Save方法执行对用户添加操作。
下面我贴出相关代码:
public class ActionServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
String flag = req.getParameter("flag");
String path = req.getServletPath();
String formName = path.substring(0,path.indexOf("."));
Action action = InitResource.getActionByPath(formName);
try {
ActionFather father = (ActionFather)Class.forName(action.getType()).newInstance();
String className = InitResource.getTypeByName(action.getName());
System.out.println(className);
Object obj = this.getRequest(Class.forName(className), req);
if(flag!=null){
father.execute(action.getType(),flag,obj, req, resp);
}else{
father.execute(obj, req, resp);
}
} catch (dException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected Object getRequest(Class cls,HttpServletRequest req){
Object obj = null;
try {
obj = cls.newInstance();
Field[] fileds = cls.getDeclaredFields();
for (Field field : fileds) {
String name = field.getName();
Method method = cls.getDeclaredMethod("set"
+ name.substring(0, 1).toUpperCase()
+ name.substring(1), field.getType());
method.invoke(obj, new Object[]{ConvertUtils.convert(req.getParameter(name),field.getType())});
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
}
public abstract class ActionFather {
public abstract void execute(Object obj, HttpServletRequest request, HttpServletResponse response);
/**
* 执行flag传进来的方法
* @param cls
* @param methodName
* @param o
* @param request
* @param response
*/
public void execute(String cls ,String methodName, Object o,HttpServletRequest request, HttpServletResponse response){
try {
Object obj = Class.forName(cls).newInstance();
Method method = Class.forName(cls).getDeclaredMethod(
methodName,new Class[]{Object.class,HttpServletRequest.class,HttpServletResponse.class});
method.invoke(obj, new Object[]{o,request,response});
}catch(Exception ex){
throw new RuntimeException("没有找到"+methodName+"方法\n"+ex.getMessage());
}
}
public Object fillField(Class cls,Object o){
Object obj = null;
try {
obj = cls.newInstance();
Field[] fields = o.getClass().getDeclaredFields();
for (Field field : fields) {
String fieldName = field.getName();
Method method = o.getClass().getDeclaredMethod("get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1));
Object value = method.invoke(o);
Method methodout = cls.getDeclaredMethod("set"
+ fieldName.substring(0, 1).toUpperCase()
+ fieldName.substring(1), field.getType());
methodout.invoke(obj, value);
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return obj;
}
}
public class UserinfoAction extends ActionFather{
private UserinfoService service = new UserinfoService();
public void setService(UserinfoService service) {
this.service = service;
}
@Override
public void execute(Object obj, HttpServletRequest request,
HttpServletResponse response) {
// TODO Auto-generated method stub
}
public void save(Object obj, HttpServletRequest request,
HttpServletResponse response) {
UserinfoForm userForm = (UserinfoForm)obj;
try {
Userinfo user = (Userinfo)fillField(Userinfo.class, userForm);
int r = service.save(user);
if(r>0){
response.sendRedirect("ok.jsp");
}else{
response.sendRedirect("error.jsp");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
simon
---------------------------
QQ:282952173