自己编写一个struts





项目需要导入两个jar包:

struts-1.3.10-all\struts-1.3.10\lib\commons-beanutils-1.8.0.jar

struts-1.3.10-all\struts-1.3.10\lib\commons-logging-1.0.4.jar


/***************************************

@1:先写一个FormUtil类用来填充ActionForm:

***************************************/

package blog.base;


import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;


import javax.jms.Session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


public class FormUtil {
public static void fillForm(HttpServletRequest request,ActionFormBean actionFormBean){
//1.实例化ActionForm的子类
//2.得到所有的参数名称
//3.将客户端所有的参数名与form bean的每一个属性名进行匹配
//4.如果匹配将从客户端得到的值填充到form bean对应的属性中,然后把form bean保存到session当中
try {
//1.实例化ActionForm的子类
ActionForm actionForm = (ActionForm)Class.forName(actionFormBean.getType()).newInstance();

//2.得到所有的参数名称
java.util.Enumeration parameterEnumeration = request.getParameterNames();

try {
//得到指定类的所有属性
PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(actionForm.getClass()).getPropertyDescriptors();

//3.将客户端所有的参数名与form bean的每一个属性名进行匹配
while (parameterEnumeration.hasMoreElements()) {
String parameter = (String) parameterEnumeration.nextElement();
for(PropertyDescriptor propertyDescriptor:propertyDescriptors){
if(propertyDescriptor.getName().equals(parameter)){
//给属性赋值
//4.如果匹配将从客户端得到的值填充到form bean对应的属性中,然后把form bean保存到session当中
String value = request.getParameter(parameter).toString();
/*propertyDescriptor.setValue(parameter, value);

System.out.println("属性名称:" + parameter + "\t值:" + propertyDescriptor.getValue(parameter));*/

org.apache.commons.beanutils.BeanUtils.setProperty(actionForm,propertyDescriptor.getName() , value);

}
}
}
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
HttpSession session = request.getSession();
session.setAttribute(actionFormBean.getName(), actionForm);
System.out.println("in FormUtil ----- actionForm :" + actionForm.toString());

} catch (InstantiationException e) {


e.printStackTrace();
} catch (IllegalAccessException e) {


e.printStackTrace();
} catch (ClassNotFoundException e) {


e.printStackTrace();
}
}
}



/***************************************

@2:Action:

***************************************/

package blog.base;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class Action {

public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response)
throws Exception  {
return null; 
}


public ActionForward execute(ActionMapping actionMapping,
ActionFormBean studentForm, HttpServletRequest request,
HttpServletResponse response) {
return null;
}
}




/***************************************

@3:ActionForm:

***************************************/

package blog.base;


import javax.servlet.http.HttpServletRequest;


public class ActionForm {

public void reset(ActionMapping mapping, HttpServletRequest request){}

public String validate(ActionMapping mapping, HttpServletRequest request){return null;}
}



/***************************************

@4:ActionFormBean:

***************************************/

package blog.base;


public class ActionFormBean {
private String name;
private String type;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}

public ActionFormBean(){

}

public ActionFormBean(String name,String type){
this.name = name;
this.type = type;
}

@Override
public String toString() {
return "ActionFormBean [name=" + name + ", type=" + type + "]";
}

}




/***************************************

@5:ActionForward:

***************************************/



package blog.base;


public class ActionForward {
private String name;
private String path;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}

public ActionForward(){

}

public ActionForward(String name,String path) {
this.name = name;
this.path = path;
}

@Override
public String toString() {
return "ActionForward [name=" + name + ", path=" + path + "]";
}

}




/***************************************

@6:ActionMapping:

***************************************/


package blog.base;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;






public class ActionMapping {
private String path;
private String type;
private String name;
private String validate;
private HashMap<String,ActionForward> forwards = new HashMap<String,ActionForward>();;

public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValidate() {
return validate;
}
public void setValidate(String validate) {
this.validate = validate;
}

public HashMap<String,ActionForward> getForwards() {
return forwards;
}

public void setForwards(HashMap<String,ActionForward> forwards) {
this.forwards = forwards;
}

public ActionForward findForward(String forwardName){


if (!forwards.isEmpty()&&forwards.containsKey(forwardName)) {
return (ActionForward)forwards.get(forwardName);
}

return null;
}

public String[] findForwards(){
ArrayList<String> forwardNameList = new ArrayList<String>();
   Set<String> forwardNames =forwards.keySet();
   while (forwardNames.iterator().hasNext()) {
    forwardNameList.add(forwardNames.iterator().next());
}
   return forwardNameList.toArray(new String[forwardNameList.size()]);

}
}



/***************************************

@7:ActionServlet:

***************************************/


package blog.base;


import java.io.IOException;
import java.util.HashMap;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import blog.AddStudentAction;


public class ActionServlet extends HttpServlet {


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ActionFormBean studentFormBean = new ActionFormBean("AddStudentForm", "cn.itcast.test.AddStudentForm");


ActionMapping actionMapping = new ActionMapping();
actionMapping.setName("studentForm");
actionMapping.setPath("/addStudent");
actionMapping.setType("cn.itcast.test.AddStudentAction");

ActionForward actionForward1 = new ActionForward();
actionForward1.setName("AddStudentSuccess");
actionForward1.setPath("/AddStudent.jsp");

ActionForward actionForward2 = new ActionForward("AddStudentFailuer","/AddStudentFailuer.jsp");
HashMap<String, ActionForward> forwards = new HashMap<String, ActionForward>();
forwards.put(actionForward1.getName(), actionForward1);
forwards.put(actionForward2.getName(), actionForward2);

actionMapping.setForwards(forwards);

FormUtil.fillForm(request, studentFormBean);

Action action = new AddStudentAction();

try {
ActionForm actionForm = (ActionForm)request.getSession().getAttribute(studentFormBean.getName());
System.out.println("in ActionServlet ---- actionFrom :" + actionForm.toString());

ActionForward actionForward = action.execute(actionMapping, actionForm, request, response);

System.out.println("in ActionServlet ---- actionForward :" + actionForward.toString());

RequestDispatcher requestDispatcher = request.getRequestDispatcher(actionForward.getPath());
requestDispatcher.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
}



/***************************************

@8:AddStudentForm:

***************************************/


package blog.test;


import javax.servlet.http.HttpServletRequest;


import blog.ActionForm;
import blog.ActionMapping;




public class AddStudentForm extends ActionForm {

private String sName;
private String major;
private int score;

public String getsName() {
return sName;
}

public void setsName(String sName) {
this.sName = sName;
System.out.println("in AddStudentForm setsName");
}

public String getMajor() {
return major;
}

public void setMajor(String major) {
this.major = major;
System.out.println("in AddStudentForm setMajor");
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
System.out.println("in AddStudentForm setScore");
}

@Override
public String toString() {
return "AddStudentForm [major=" + major + ", sName="
+ sName + ", score=" + score + "]";
}

public AddStudentForm() {
System.out.println("in AddStudentForm constructor");
}

@Override
public void reset(ActionMapping mapping, HttpServletRequest request) {
System.out.println("in AddStudentForm reset");
}

@Override
public String validate(ActionMapping mapping,
HttpServletRequest request) {
System.out.println("in AddStudentForm validate");
return "";//null 等同于super.validate(mapping, request)
}
}





/***************************************

@9:

AddStudentAction :

***************************************/

package blog.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import blog.Action;
import blog.ActionForm;
import blog.ActionForward;
import blog.ActionMapping;


public class AddStudentAction extends Action {

public AddStudentAction() {
System.out.println("in AddStudentAction constructor 测试Action的实例个数");
}

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

AddStudentForm studentForm = (AddStudentForm) form;

String returnURLKeyWord = "AddStudentFailuer";

if (studentForm!=null) {
System.out.println(studentForm.getsName());
returnURLKeyWord = "AddStudentSuccess";
}

return mapping.findForward(returnURLKeyWord);
}

}



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>cn.itcast.base.ActionServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>/addStudent</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
  <welcome-file>AddStudent.jsp</welcome-file>
  <welcome-file>Login.jsp</welcome-file>    
  </welcome-file-list>
  
  <login-config>
  <auth-method>BASIC</auth-method>
 </login-config>
  
</web-app>



另外还有两个jsp页面:AddStudent.jsp,AddStudentFailuer.jsp






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值