DynaActionForm的使用与参数
DynaActionForm为ActionForm的一种动态实现,它能实现多种ActionForm的功能
1.login.jsp(登录界面)
<%@ page language="java" pageEncoding="GB18030"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%> <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html:html lang="true"> <head> <title>login.jsp</title> </head> <body> <html:form action="/login" method="post"> 用户名:<html:text property="username"></html:text> <br> 密 码:<html:text property="password"></html:text> <br> <html:submit>提交</html:submit> </html:form> </body> </html:html>
2.struts-config.xml(配置文件,实现DynaActionForm并提供参数)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <data-sources /> <form-beans > <form-bean name="loginForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" 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 > <action attribute="loginForm" input="/form/login.jsp" name="loginForm" path="/login" scope="request" type="com.feng.struts.action.LoginAction" > </action> </action-mappings> <message-resources parameter="com.feng.struts.ApplicationResources" /> </struts-config>
3.loginAction(处理类,继承Action)
package com.feng.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; public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaActionForm loginForm = (DynaActionForm) form; String username = (String) loginForm.get("username"); String password = loginForm.getString("password"); if (username.equals("feng") && password.equals("619")) { request.getSession().setAttribute("username", username); return new ActionForward("/success.jsp"); } return mapping.getInputForward();//跳到输入界面,即表单界面 } }
4.success.jsp(跳转页面)
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> <title>Insert title here</title> </head> <body> <h2> ${username }登录成功 </h2> </body> </html>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛