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

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

分类:Java 时间:2010-04-01 人气:251
分享到:
blog comments powered by Disqus

相关文章

  • Producer and consumer issues to achieve product category Java: public class Product (private int id; priv 2010-05-27

    Producer and consumer issues Java implementation Product category: public class Product ( private int id; private String value; public Product (int id, String value) ( this.id = id; this.value = value; ) public int getId () ( return id; ) public void ...

  • java data type conversion Integer String Long Float Double Date 2010-07-24

    1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Integer.valueOf (my_str). IntValue (); Note: The string converted to Double, Float, Lo

  • java data type conversion (change) 2010-09-01

    Transfer from http://hi.baidu.com/loveland/blog/item/eb2d96456e64d13a8694737b.html 1 How to convert integer string String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt ([String], [int radix]); 2). Int i = Int

  • Common data type conversion in JAVA 2010-09-30

    JAVA common data type conversion functions in the January 7, 2008 Monday 10:58 JAVA integer and string conversion How to convert a string into an integer String int? A. There are two ways: 1). Int i = Integer.parseInt ([String]); or i = Integer.parseInt (

  • JAVA GUI in a text box (JTextField) to monitor changes in the text box 2010-10-29

    As a cross-platform programming solution, Java is clearly not like the Delphi, as the user interface design to provide powerful, intuitive and efficient design support. For example, a text box control, when we perceived in the program if the user changes

  • In java, you will utf-8 character code mapped into the Chinese 2010-03-29

    The following is a character code conversion tool. In this leave a mark can only be run under jdk1.6. utf16 this class jre / lib / rt.jar under. /* * (#)CharsetConvertor.java 1.0 2009-8-19 */ package com.codelib.util; import java.io.UnsupportedEncodingExc

  • java data type conversion Xiangjie 2010-03-29

    Java data types are divided into three categories, namely, the Boolean type, character and numeric type, which are divided into numeric integer and floating-point type; relative to the data types, Java variable of type Boolean boolean; character char ...

  • Of Non-Struts for Java Web Development 2009-02-18

    Java Web development project, if you do not use Struts, WebWork, SpringMVC other MVC framework, using the original Servlet API, the how to develop it? Struts 1.x uses a "total control" role of the Servlet to handle all the requests, and Str ...

  • java data type conversion 2010-12-05

    Some beginners may encounter friends JAVA JAVA data type conversion between the distress, for example, integer and float, double type conversion between integer and String conversion between types, as well as processing, display timing of the problem

  • JAXB of Eclipse plug-ins (. Xsd file to the java class mapping) 2010-09-14

    1, JAXB's Eclipse plug-in download address: https: / / jaxb-workshop.dev.java.net/servlets/ProjectDocumentList? FolderID = 4962 & expandFolder = 4962 & folderID = 0 Second, plug-in instructions: https: / / jaxb-workshop.dev.java.net/plugins/ec

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.573 (s). 14 q(s)