Struts中静态动态ActionForm的使用(一)

作者:上海建筑防水有限公司  来源:www.yxjzfs.com   发布时间:2017-09-09 10:30:14
Struts中静态动态ActionForm的使用(一) 目录

--------------------------------------------------------------------------------

登录例子

区别

时间属性

验证机制

--------------------------------------------------------------------------------

在Struts1中,我们经常接触ActionForm为静态ActionForm。静态是指开发者在使用struts框架时必须写对应的ActionForm。

在介绍动态ActionForm使用之前,我们回忆struts常用的ActionForm的方式,以便做对比。

举个完整的登录例子。

第一:配置web.xml

[html]

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<servlet>

<servlet-name>action</servlet-name>

<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping> 第二:新建jsp页面(在login文件夹)

[html] view plaincopyprint?

<form action="../login.do" method="get">

用户名: <input name="username" type="text"><br>

密 码: <input name="password" type="text"><br>

<input type="submit" >

</form>

<form action="../login.do" method="get">

用户名: <input name="username" type="text"><br>

密 码: <input name="password" type="text"><br>

<input type="submit" >

</form>

第三:新建ActionForm

[html]

package com.login;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

public class LoginForm extends ActionForm{

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

private String username;

private String password;

}

package com.login;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionMessage;

import org.apache.struts.action.ActionMessages;

public class LoginForm extends ActionForm{

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

private String username;

private String password;

} 第四:新建Action

[html]

package com.login;

import java.util.Date;

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) throws Exception {

LoginForm loginForm=(LoginForm)form;

boolean flag=false;

if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){

flag=true;

}

request.setAttribute("username",loginForm.getUsername());

if(flag)

{

return mapping.findForward("success");

}else{

return mapping.findForward("false");

}

}

}

package com.login;

import java.util.Date;

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) throws Exception {

LoginForm loginForm=(LoginForm)form;

boolean flag=false;

if("admin".equals(loginForm.getUsername())&&"admin".equals(loginForm.getPassword())){

flag=true;

}

request.setAttribute("username",loginForm.getUsername());

if(flag)

{

return mapping.findForward("success");

}else{

return mapping.findForward("false");

}

}

} 第五:配置struts核心配置文件struts-config.xml

[html]

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<!-- 静态form -->

<form-beans>

<form-bean name="loginForm" type="com.login.LoginForm"></form-bean>

</form-beans>

<action-mappings>

<action path="/login" type="com.login.LoginAction" name="loginForm"

scope="request">

<forward name="success" path="/login/success.jsp"></forward>

<forward name="false" path="/login/false.jsp"></forward>

</action>

</action-mappings>

</struts-config>

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"

"http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>

<!-- 静态form -->

<form-beans>

<form-bean name="loginForm" type="com.login.LoginForm"></form-bean>

</form-beans>

<action-mappings>

<action path="/login" type="com.login.LoginAction" name="loginForm"

scope="request">

<forward name="success" path="/login/success.jsp"></forward>

<forward name="false" path="/login/false.jsp"></forward>

</action>

</action-mappings>

</struts-config> 使用以上常用方式,最大的麻烦就是,几乎为每个表单创建一个ActionForm。导致ActionForm数量多。

而使用动态ActionForm,最大的优点就是,不必手动为每个表单创建ActionForm。

使用动态ActionForm的方式:

上述的第一步、第二步不变、第三步取消、修改第四步、第五步。

为了让方便大家理解,首先修改第五步:

[html] 、

<!-- 动态form -->

<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>

<!-- 动态form -->

<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>

第五步中使用form-property指明表单中的属性,其中标签内的name是表单jsp中的控件name值,两者必须相同。type是指控件值的类型。

修改第四步:

[html]

package com.login;

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) throws Exception {

DynaActionForm loginForm=(DynaActionForm)form;

String username=(String)loginForm.get("username");

String password=(String)loginForm.get("password");

boolean flag=false;

if("admin".equals(username)&&"admin".equals(password)){

flag=true;

}

request.setAttribute("username",username);

if(flag)

{

return mapping.findForward("success");

}else

{

return mapping.findForward("false");

}

}

}

package com.login;

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) throws Exception {

DynaActionForm loginForm=(DynaActionForm)form;

String username=(String)loginForm.get("username");

String password=(String)loginForm.get("password");

boolean flag=false;

if("admin".equals(username)&&"admin".equals(password)){

flag=true;

}

request.setAttribute("username",username);

if(flag)

{

return mapping.findForward("success");

}else

{

return mapping.findForward("false");

}

}

}

静态ActionForm方式,使用get/set方法,而动态ActionForm方式,使用的map的get key方式。其中的key就是标签name值。

使用动态ActionForm的另一个优点是,,若是更改表单和ActionForm时,不需要重新部署。而使用静态ActionForm时,则需要修改ActionForm.java文件,还必须重新部署。

但是使用动态ActionForm,配置文件太长。不如静态ActionForm清晰明了。

使用时间属性

动态ActionForm使用时间类型

第一种方式:使用java.sql.Date类型。但是界面必须是yyyy-MM-dd这种形式。

第二种方式:使用String类型。但是在后台自己转换。

静态ActionForm使用时间类型

ActionForm有自行匹配,检验的功能。自动把表单上的数据转化成ActionForm相应的数据类型。但是默认的java.sql.Date类型,但是对于java.util.Date类型则不能自动转化。

所以为了使用java.util.Date类型,我们手动开发时间转化类,然后注入到框架中

[html]

package com.util;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class UtilDateConvert implements Converter {

@Override

public Object convert(Class type, Object value) {

if(value==null){

return value;

}

if(value instanceof Date){

return value;

}

Date dateConvert=null;

if(value instanceof String){

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");

try {

dateConvert =(Date)sdf.parse((String)value);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return dateConvert;

}

}

package com.util;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class UtilDateConvert implements Converter {

@Override

public Object convert(Class type, Object value) {

if(value==null){

return value;

}

if(value instanceof Date){

return value;

}

Date dateConvert=null;

if(value instanceof String){

SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd");

try {

dateConvert =(Date)sdf.parse((String)value);

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return dateConvert;

}

}

其中转化类中定义什么格式,界面输入什么格式。如上文中,定义yyyy/MM/dd 界面输入1988/10/11.若输入1988-10-11格式,则提示转化错误。另外转化类中可以定义yyyy-MM-dd格式。

[html]

package com.util;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import org.apache.commons.beanutils.ConvertUtils;

public class UtilDateServlet extends HttpServlet {

@Override

public void init() throws ServletException {

// TODO Auto-generated method stub

ConvertUtils.register(new UtilDateConvert(), Date.class);

}

}

package com.util;

import java.util.Date;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import org.apache.commons.beanutils.ConvertUtils;

public class UtilDateServlet extends HttpServlet {

@Override

public void init() throws ServletException {

// TODO Auto-generated method stub

ConvertUtils.register(new UtilDateConvert(), Date.class);

}

}

在配置文件中配置servlet

[html]

<servlet>

<servlet-name>dateConvert</servlet-name>

<servlet-class>com.util.UtilDateServlet</servlet-class>

<load-on-startup>2</load-on-startup>

</servlet>

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:咸宁网站建设 http://xianning.45qun.com


上一篇:在Jenkins的Python脚本中模拟用户输入
下一篇:最后一页