Strust 2类型转化器练习2--局部类型转换器

 代码:

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>长方形</title>
<style>
.content {
	font-size: 12px;
}

#search_botton {
	border: 1px solid #8CC6FF;
	color: #0066CC;
	padding: 1px 1px;
	background-color: #f1f9ff;
}
</style>
</head>
<body>
   <s:form action="computeArea">
      <table border="0" class="content">
        <tr>
          <td><s:textfield name="rectangle" label="长方形长和宽是(中间“,”隔开)"/></td>
        </tr>
        
        <tr>
          <td><s:submit value="提交" id="search_botton"></s:submit></td>
        </tr>
      </table>
   </s:form>
</body>
</html>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Rectangle</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>rectangle.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
	<filter-name>struts2Controller</filter-name>
	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
	 <filter-name>struts2Controller</filter-name>
	 <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

Strust.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
 <package  name="demo1" extends="struts-default">
	 <action name="computeArea" class="com.action.AreaAction">
	  	<result name="success">/showRectangle.jsp</result>
	 </action>
  </package>
</struts>

showRectangle.jsp

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<style>
.content {
	font-size: 12px;
}
</style>
</head>
<body>
<table class="content">
  <tr>
	<td>长方形的面积是:<s:property value="rectangle" /></td>
  </tr>
</table>
</body>
</html>

AreaAction.java

package com.action;

import com.diman.Rectangle;
import com.opensymphony.xwork2.ActionSupport;

public class AreaAction extends ActionSupport {
  private Rectangle rectangle;

public Rectangle getRectangle() {
	return rectangle;
}

public void setRectangle(Rectangle rectangle) {
	this.rectangle = rectangle;
}

public String execute() throws Exception {
	// TODO Auto-generated method stub
	return "success";
}
  
}

Rectangle.java

package com.diman;

public class Rectangle {
  private double length;
  private double width;
  private double area;
public double getLength() {
	return length;
}
public void setLength(double length) {
	this.length = length;
}
public double getWidth() {
	return width;
}
public void setWidth(double width) {
	this.width = width;
}
public double getArea() {
	return area;
}
public void setArea(double area) {
	this.area = area;
}
  
}

RectangleConverter.java

package com.converter;

import java.lang.reflect.Member;
import java.util.Map;
import com.diman.Rectangle;
import ognl.DefaultTypeConverter;
public class RectangleConverter extends DefaultTypeConverter {

	/*
 	 * context 是类型转换环境的上下文
	 * value 是须要转换的参数,方向不同,value的类型也是不同的.
	 * toType是转换后的目标类型,
            * 返回值是 : 转换后的目标类型, 方向不同,类型也是不同的.
 	 */
    public Object convertValue(final Map context, 
          final Object value, final Class toType){
	 	//从表单字符串转换为 Rectangle对象
        if (Rectangle.class == toType)
        {
            final Rectangle rectangle = new Rectangle();
            final String[] str = (String[]) value;
            final String[] param = str[0].split(",");
            final double length = Double.parseDouble(param[0]);
            final double width = Double.parseDouble(param[1]);

            rectangle.setLength(length);
            rectangle.setWidth(width);
            return rectangle;
        }
        //从对象转换为字符串
        if (String.class == toType)
        {
            final Rectangle rectangle = (Rectangle) value;
            final double length = rectangle.getLength();
            final double width = rectangle.getWidth();
            final String area = (length*width)+"";
            return area;
        }
        return null;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇潇雨歇_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值