代码:
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;
}
}