1.新建FormBean
//
生成get和set方法
2.新建ActionMapping
// <action path="/login"type="com.it315.action.LoginAction"
name = "loginForm" >
<!—name是forward标签的唯一
<forward
name="success" path="/Success.jsp"></forward>
<forward
name="errpr" path="/Error.jsp"></forward>
</action>
private String path;
private String name;
private String type;
private Map<String,ActionForward> forward
= new HashMap< String,ActionForward >();
生get和set方法
3.新建ActionForward
private String name;
private String path;
生get和set方法
4.在2步骤中的ActionMapping中新建Map
5.新建ServletActionServlet
重写init,在其中
//读取Struts-config.xml,离不开dom4j的内容,当然struts不是用dom4j的
String path
= this.getServletConfig().getInitParameter(“config”);
this.getServletContext().getRealPath(path);
Document
document = this.getDocument(path);
String
xpath = “/struts-config/form-beans”
6.新建Action
package com.itcast.struts.sys;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Action {
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,
HttpServletResponse response){
return null;
}
}
7.新建ActionForm
package com.itcast.struts.sys;
public abstract class ActionForm {
}
8.新建LoginAction
package com.itcast.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itcast.struts.form.LoginForm;
import com.itcast.struts.sys.Action;
import com.itcast.struts.sys.ActionForm;
import com.itcast.struts.sys.ActionForward;
import com.itcast.struts.sys.ActionMapping;
public class LoginAction extends Action {
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
System.out.println("form "+form);
LoginForm loginForm=(LoginForm)form;
System.out.println(loginForm.getUsername());
if("sa".equals(loginForm.getUsername())){
return mapping.findForward("success");
}
return mapping.findForward("error");
}
}
9.新建LoginForm
package com.itcast.struts.form;
import com.itcast.struts.sys.ActionForm;
public class LoginForm extends ActionForm{
private String username;
private String psw;
public String getUsername() {
return username;
}
public void setUsername(String username) {
System.out.println("username "+username);
this.username = username;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
System.out.println("psw "+psw);
this.psw = psw;
}
}
10.配置Struts-config.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.itcast.struts.sys.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
10.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>com.itcast.struts.sys.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
10.新建ActionServlet
package com.itcast.struts.sys;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class ActionServlet extends HttpServlet {
/*
* <form-bean name="loginForm" type="com.itcast.struts.form.LoginForm"></form-bean>
* map<name,new FormBean()>
*/
private Map<String,FormBean> FormBeanMap=new HashMap<String,FormBean>();
/*
* <action path="/login" name="loginForm"
* type="com.itcast.struts.action.LoginAction" scope="request">
* map<path,new ActionMapping()>
*/
private Map<String,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();
private static final long serialVersionUID = 1L;
/**
* 服务器启动读取struts-config.xml文件
*/
public void init() throws ServletException {
String filepath=this.getServletConfig().getInitParameter("config");
filepath=this.getServletContext().getRealPath(filepath);
System.out.println(filepath);
//获取Document文档
Document document=this.getDocument(filepath);
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
* 加载form-bean
* <form-bean name="loginForm" type="com.itcast.struts.form.LoginForm"></form-bean>
*/
String xpath="/struts-config/form-beans/form-bean";
List<Node> formBeanList=document.selectNodes(xpath);
if(formBeanList!=null&&formBeanList.size()>0){
for(int i=0;i<formBeanList.size();i++){
Element formBeanElement=(Element) formBeanList.get(i);
String name=formBeanElement.attributeValue("name");
String type=formBeanElement.attributeValue("type");
FormBean formBean=new FormBean();
formBean.setName(name);
formBean.setType(type);
FormBeanMap.put(name, formBean);
}
}
// //遍历
// Collection<FormBean> c=FormBeanMap.values();
// List<FormBean> list=new ArrayList<FormBean>(c);
// for(int i=0;i<list.size();i++){
// System.out.println(list.get(i).getName()+" "+list.get(i).getType());
// System.out.println(FormBeanMap.get(list.get(i).getName()).getType());
// }
//
//////////////////////////////////////////////////////////////////////////////////////////////////
//<action path="/login" name="loginForm" type="com.itcast.struts.action.LoginAction" scope="request">
xpath="/struts-config/action-mappings/action";
List<Node> actionMapingList=document.selectNodes(xpath);
if(actionMapingList!=null&&actionMapingList.size()>0){
for(int i=0;i<actionMapingList.size();i++){
//<action>
Element actionMapingElement=(Element) actionMapingList.get(i);
String path=actionMapingElement.attributeValue("path");
String name=actionMapingElement.attributeValue("name");
String type=actionMapingElement.attributeValue("type");
System.out.println(path +" "+name +" "+type);
ActionMapping mapping=new ActionMapping();
mapping.setName(name);
mapping.setPath(path);
mapping.setType(type);
actionMapingMap.put(path, mapping);
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
* <action path="/login" name="loginForm" type="com.itcast.struts.action.LoginAction" scope="request">
<!-- 配置转发或重定向 默认值是转发-->
<!--
name 属性:表示forword标签的唯一标识
path 属性:表示要转发或重定向的路径
-->
<forward name="success" path="/success.jsp"></forward>
*/
List<Element> forwardList=actionMapingElement.elements("forward");
if(forwardList!=null&&forwardList.size()>0){
for(int k=0;k<forwardList.size();k++){
Element forwardElement=forwardList.get(k);
String forward_name=forwardElement.attributeValue("name");
String forward_path=forwardElement.attributeValue("path");
System.out.println(forward_name+" "+forward_path);
ActionForward actionForward=new ActionForward();
actionForward.setName(forward_name);
actionForward.setPath(forward_path);
mapping.addActionForward(actionForward);
}
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
//遍历private Map<String,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();
List<ActionMapping> list=new ArrayList(actionMapingMap.values());
for(int i=0;i<list.size();i++){
ActionMapping m=list.get(i);
System.out.println("m.getName() "+m.getName());
System.out.println("m.getPath() "+m.getPath());
System.out.println("m.getType() "+m.getType());
List<ActionForward> l=new ArrayList(m.getForward().values());
for(int k=0;k<l.size();k++){
ActionForward a=l.get(k);
System.out.println("^^^ "+a.getName()+ " " +a.getPath());
}
}
}
public Document getDocument(String path){
Document document=null;
SAXReader read=new SAXReader();
try {
document=read.read(new File(path));
} catch (DocumentException e) {
e.printStackTrace();
}
return document;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//////////////////////////////////////////////////////////////////////////////////////////////////
//获取请求路径
String servletPath=request.getServletPath();
// /login.do ---->/login
//System.out.println("servletPath "+servletPath);
servletPath=servletPath.substring(0,servletPath.lastIndexOf("."));
System.out.println("servletPath "+servletPath); ///login
//////////////////////////////////////////////////////////////////////////////////////////////////
//通过解析请求路径获取 ActionMapping
//private Map<path,ActionMapping> actionMapingMap=new HashMap<String,ActionMapping>();
ActionMapping mapping=actionMapingMap.get(servletPath);
//System.out.println(maping.getPath());
//System.out.println(maping.getName());
//System.out.println(maping.getType());
String formBeanName=mapping.getName();
//通过name属性查找FormBean
//private Map<String,FormBean> FormBeanMap=new HashMap<String,FormBean>();
FormBean formBean=FormBeanMap.get(formBeanName);
System.out.println(formBean.getName()+" "+formBean.getType());
//////////////////////////////////////////////////////////////////////////////////////////////////
//login.jsp页面的数据填充的ActionForm(LoginForm)中
//获取FormBean中type属性的值
String formBean_type=formBean.getType(); // type=com.itcast.struts.form.LoginForm
//创建com.itcast.struts.form.LoginForm类的实例
try {
Object obj=Class.forName(formBean_type).newInstance();
//转化到ActionForm
ActionForm actionForm=(ActionForm)obj;
//获取actionForm该对象的类表示
Class clazz=actionForm.getClass();
//获取类中所有的方法
Method[] method=clazz.getMethods();
//存放set方法的集合
List<Method> listMehtod=new ArrayList<Method>();
//获取类中的set方法
if(method!=null&&method.length>0){
for(int i=0;i<method.length;i++){
Method m=method[i];
System.out.println("^^^$$$$$$^^^^^^^m "+m.getName());
//方法的名称是以set
if(m.getName().startsWith("set")){
listMehtod.add(m);
}
}
}
//遍历所有set方法
if(listMehtod.size()>0){
for(int i=0;i<listMehtod.size();i++){
Method m=listMehtod.get(i);
String methodname=m.getName();
System.out.println("^^^^^^^^^^methodname "+methodname); //setUsername
//获取属性
String params=methodname.substring(3,4).toLowerCase()+methodname.substring(4);
System.out.println("params "+params);
//调用的是set方法 public void setUsername(String username)
m.invoke(actionForm, request.getParameter(params));
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//获取type属性
String type=mapping.getType();
System.out.println("type "+type);
Action action=(Action)Class.forName(type).newInstance();
ActionForward actionForward=action.execute(mapping, actionForm, request, response);
System.out.println("actionForward.getPath() "+actionForward.getPath());
//重定向
//response.sendRedirect(location)
//转发
request.getRequestDispatcher(actionForward.getPath()).forward(request, response);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
11.新建success.jsp和error.jsp 这简单就不赘述!
posted on 2010-01-15 02:57
d66380022 阅读(1887)
评论(0) 编辑 收藏