Java:struts Frame 5 (converter,validation,tags)

Source: Internet
Author: User

1.Converter:

Struts.xml:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.3//en" "Http://struts.apache.or G/dtds/struts-2.3.dtd "><Struts>    < Packagename= "Converterdemo"extends= "Struts-default">        <Actionname= "point-*"class= "Cn.zzsxt.action.MyPointAction"Method= "{1}">            <resultname= "Success">/success.jsp</result>        </Action>    </ Package></Struts>

Conversion:

 Packagecn.zzsxt.conversion;ImportJava.util.Map;ImportOrg.apache.struts2.util.StrutsTypeConverter;ImportCn.zzsxt.entity.MyPoint; Public classMypointconverterextendsStrutstypeconverter {/*** 10,20 * Convert string to Composite type * Context: Context * Values: Parameter's value * ToType: Target type*/@Override PublicObject convertfromstring (Map context, string[] values, Class totype) {if(Totype==mypoint.class) {String value= Values[0];//gets the value of the coordinate: 10,20string[] arr = Value.split (",");//arr = {"10,20"}//gets the value of the x-coordinate            intx = Integer.parseint (arr[0]); inty = Integer.parseint (arr[1]); MyPoint MyPoint=NewMyPoint ();            Mypoint.setx (x);            Mypoint.sety (y); returnMyPoint; }        return NULL; }    /*** Convert a composite type to a string*/@Override PublicString converttostring (Map arg0, Object arg1) {return NULL; }}

Action

 Packagecn.zzsxt.action;ImportCom.opensymphony.xwork2.ActionSupport;ImportCn.zzsxt.entity.MyPoint; Public classMypointactionextendsActionsupport {PrivateMyPoint Point;  Publicmypoint GetPoint () {returnPoint ; }     Public voidsetpoint (MyPoint point) { This. Point =Point ; }         PublicString Add ()throwsException {System.out.println ("The Add method is executed with the coordinates:" +Point ); return  This.    SUCCESS; }}

Action Package properties file (Simple Type):

Point=cn.zzsxt.conversion.mypointconverter

  

Conversion Package properties File (composite type):

Cn.zzsxt.entity.mypoint=cn.zzsxt.conversion.mypointconverter

INDEX.JSP:

  

<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <Basehref= "<%=basePath%>">    <title>My JSP ' index.jsp ' starting page</title>  </Head>    <Body>      <formAction= "Point-add.action"Method= "POST">Please enter coordinates (x, y)<inputtype= "text"name= "point"><BR>          <inputtype= "Submit"value= "Submit">      </form>  </Body></HTML>

SUCCESS.JSP:

<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%><%StringPath=Request.getcontextpath ();StringBasePath=Request.getscheme ()+"://"+Request.getservername ()+":"+Request.getserverport ()+Path+"/";%><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"><HTML>  <Head>    <Basehref= "<%=basePath%>">    <title>My JSP ' index.jsp ' starting page</title>  </Head>    <Body>X=${point.x}<BR>Y=${point.y}<BR>  </Body></HTML>

Java:struts Frame 5 (converter,validation,tags)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.