三大框架之Struts

一、动态ActionForm

1.配置struts环境


2.创建一个new.jsp

<%@ page contentType="text/html;charset=gb2312"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<html:html lang="true">
  <body>
    <html:form action="news.do" method="post">
    <html:text property="user"></html:text>
    <html:password property="password"></html:password>
    <html:submit value="提交"></html:submit>
    </html:form>
  </body>
</html:html>

3.在struts-config.xml中配置ActionForm的2个属性:user、password

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
  <form-bean name="newsForm" type="org.apache.struts.action.DynaActionForm">
  <form-property name="user" type="java.lang.String"></form-property>
  <form-property name="password" type="java.lang.String"></form-property>
  </form-bean>
  </form-beans>

  <global-exceptions />
  <global-forwards />
  <action-mappings />
  <message-resources parameter="com.learn.struts.ApplicationResources" />
</struts-config>

4.通过Eclipse,来创建NewsAction.java

点击window,找到customize perspective,进行设置


在src右键new,找到struts 1.3 action,进行设置


5.对struts-config.xml再次进行设置

在<action-mappings>之间添加如下代码

 <action-mappings >
    <action
      path="/news"
      scope="request"
      type="com.learn.struts.action.NewsAction"
      attribute="newsForm" 
      name="newsForm"
      input="/new.jsp"
       />
  </action-mappings>

6.在NewsAction.java类中添加代码后,运行

* Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.learn.struts.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;


/** 
 * MyEclipse Struts
 * Creation date: 09-28-2014
 * 
 * XDoclet definition:
 * @struts.action scope="request" validate="true"
 */
public class NewsAction extends Action {
/*
* Generated Methods
*/


/** 
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
DynaActionForm dynaForm=(DynaActionForm ) form;
String user=dynaForm.getString("user");
String password=dynaForm.getString("password");
System.out.println("user:"+user);
System.out.println("password:"+password);
return null;
}
}

运行结果:

http://localhost:8080/strutsProject/new.jsp







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦里仙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值