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

  • 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

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

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

  • [编程基础]Java 中的Package和Import 2013-03-20

    开始要逐步去熟悉最基础的语法了,幸好之前是有底子的,所以理解也不难, import Package 有点类似C语言中的#include [头文件] 为什么要用包(Package) 当一个大型程序交由数个不同的程序人员开发时,用到相同的类名是很有可能的,那么如果发生了这样的事件我们该怎么办那?,在我们java程序开发中为了避免上述事件,提供了一个包的概念(package),那么既然有了这样一种方法能避免上述事件,那么我们怎样使用package呢?,使用方法很简单我们只需要在我们写的程序第一行使用p

  • 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

  • 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

  • eclipse, language: java; db: db2; dbDriver: jdbc (2) 2010-06-17

    Access db2 using jdbc 1, eclipse configure the jdbc driver: Project name -> right click -> BuildPath-> Configure build path (Referenced Libraries) Added after Jdbc driver, drive in the DB2 installation directory (I installed in E: \ Program Files

  • java type conversion (summary) 2010-08-22

    java type conversion (1) String to int conversion String MyNumber ="1234"; int MyInt = Integer.parseInt(MyNumber); String into a byte, short, int, float, double, long and other data types, with reference Byte, Short, Integer, Float, Double, Long

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

processed in 0.910 (s). 12 q(s)