struts form the type conversion, convert by custom class to implement two types of conversion

sponsored links
In the previous article we realized through their own writing Convert the interface of a class, and by the time a Serverlet in the init register this Convert.

However, this realization is not good enough.

In the struts can be achieved through plug-ins.

Complete code is as follows:

convert.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="convertAction.do" method="post">
        intvalue:<input type="text" name="intval"/><br>
        doublevalue:<input type="text" name="doubleval"/><br>
        booleanvalue:<input type="text" name="booleanval"/><br>
        utildatevalue:<input type="text" name="dateval"/><br>
        <input type="submit" value="submit"/>
</form>
</body>
</html>


form.

package com.lwf.struts.form;


import java.util.Date;

import org.apache.struts.action.ActionForm;

public class ConvertForm extends ActionForm {
        private int intval;
        private double doubleval;
        private boolean booleanval;
        private Date dateval;
        
        public int getIntval() {
                return intval;
        }
        public void setIntval(int intval) {
                this.intval = intval;
        }
        public double getDoubleval() {
                return doubleval;
        }
        public void setDoubleval(double doubleval) {
                this.doubleval = doubleval;
        }
        public boolean isBooleanval() {
                return booleanval;
        }
        public void setBooleanval(boolean booleanval) {
                this.booleanval = booleanval;
        }
        public Date getDateval() {
                return dateval;
        }
        public void setDateval(Date dateval) {
                this.dateval = dateval;
        }
}


action

package com.lwf.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;

public class ConvertAction extends Action {

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

}


Results:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

        intvalue:${convertForm.intval }<br>
        doublevalue:${convertForm.doubleval }<br>
        booleanvalue:${convertForm.booleanval }<br>
        utildatevalue:${convertForm.dateval }<br>
        </body>
</html>


Configuration:

<action path="/convertAction" type="com.lwf.struts.action.ConvertAction" name="convertForm"  scope="request">
                        <forward name="success" path="/convertResult.jsp"></forward>
                </action>


<form-bean name="convertForm" type="com.lwf.struts.form.ConvertForm"></form-bean>


Here is a custom convert class, used to convert the java.util.Date type String class

package com.lwf.struts.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class UtilDateConverter implements Converter {

        public UtilDateConverter(Object object) {
        }

        public Object convert(Class type, Object value) {
                
                if(value == null){
                        return value;
                }
                        
                if(value instanceof Date){
                        return value;
                }
                
                if(value instanceof String){
                        try {
                                SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
                                
                                return sdf.parse((String)value);
                        } catch (ParseException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        }
                }
                return null;
        }

}


The following code is the key: UtilDateConvertPlugIn used to implement the PlugIn interface, and the init method to achieve

package com.lwf.struts.util;

import java.util.Date;

import javax.servlet.ServletException;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class UtilDateConvertPlugIn implements PlugIn {

        public void destroy() {

        }

        public void init(ActionServlet arg0, ModuleConfig arg1)
                        throws ServletException {
                  ConvertUtils.register(new UtilDateConverter(null), Date.class);   
        }

}


Configuration:

<plug-in className="com.lwf.struts.util.UtilDateConvertPlugIn" ></plug-in>


This will load the Struts startup and implementation of the init method of this UtilDateConvertPlugIn. .

Is it more reasonable than to create a new servlet it. .
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of struts form the type conversion, convert by custom class to implement two types of conversion

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries