第一步:(这一步和其他一样,这里从简)
依旧是新建一个web project,命名为struts2,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。
第二步:
将index.jsp改名为input.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。Input.jap的代码如下
-
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP ‘index.jsp‘ starting page</title>
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
</head>
-
-
<body>
-
<h1><font color=‘red‘> 请输入坐标,用英文半角逗号隔开</font></h1>
-
<s:form action="pointconverter">
-
<s:textfield name="point1" label="point1"></s:textfield>
-
<s:textfield name="point2" label="point2"></s:textfield>
-
<s:textfield name="point3" label="point3"></s:textfield>
-
-
<s:submit name="submit"> </s:submit>
-
</s:form>
-
</body>
-
</html>
该文件有两个要注意的地方
1.使用了struts2的标签库 <%@ taglib prefix="s" uri="/struts-tags" %>
2.f注意form中的action属性
第三步:
在src下新建包com.beam,其中定义point类 point.java 代码如下:
-
package com.bean;
-
-
public class Point {
-
-
-
-
public int getX() {
-
return x;
-
}
-
public void setX(int x) {
-
this.x = x;
-
}
-
public int getY() {
-
return y;
-
}
-
public void setY(int y) {
-
this.y = y;
-
}
-
-
-
-
private int x;
-
private int y;
-
}
Action
在src下新建包com.action
其中新建类PointAction.java 代码如下
-
package com.action;
-
-
import com.opensymphony.xwork2.ActionSupport;
-
import com.bean.Point;
-
public class PointAction extends ActionSupport
-
{
-
-
public Point getPoint1() {
-
return point1;
-
}
-
public void setPoint1(Point point1) {
-
this.point1 = point1;
-
}
-
public Point getPoint2() {
-
return point2;
-
}
-
public void setPoint2(Point point2) {
-
this.point2 = point2;
-
}
-
public Point getPoint3() {
-
return point3;
-
}
-
public void setPoint3(Point point3) {
-
this.point3 = point3;
-
}
-
-
public String execute() throws Exception
-
{
-
return SUCCESS;
-
}
-
-
-
private Point point1;
-
private Point point2;
-
private Point point3;
-
-
}
第五步:配置struts.xml文件 代码如下:
-
<?xml version="1.0" encoding="utf-8" ?>
-
<!DOCTYPE struts PUBLIC
-
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
-
"struts.apache.org/dtds/struts-2.0.dtd">
-
-
<struts>
-
-
<package name="struts2" extends="struts-default">
-
<action name="pointconverter" class="com.action.PointAction">
-
<result name="success">/output.jsp</result>
-
<result name="input">/input.jsp</result>
-
</action>
-
</package>
-
</struts>
第六步:
在WebRoot下新建视图output.jsp 依旧运用struts2的标签库 代码如下
-
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
-
<%
-
String path = request.getContextPath();
-
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
-
%>
-
<%@ taglib prefix="s" uri="/struts-tags" %>
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
-
<html>
-
<head>
-
<base href="<%=basePath%>">
-
-
<title>My JSP ‘output.jsp‘ starting page</title>
-
-
<meta http-equiv="pragma" content="no-cache">
-
<meta http-equiv="cache-control" content="no-cache">
-
<meta http-equiv="expires" content="0">
-
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
-
<meta http-equiv="description" content="This is my page">
-
<!--
-
<link rel="stylesheet" type="text/css" href="styles.css">
-
-->
-
-
</head>
-
-
<body>
-
-
point1:<s:property value="point1"/><br>
-
point2: <s:property value="point2"/><br>
-
point3 <s:property value="point3"/>
-
</body>
-
</html>
第七步:类型转化器
在src目录下新建com.converter包 其中新建类PointConverter.java 代码如下
-
package com.converter;
-
-
import java.util.Map;
-
-
import org.apache.struts2.util.StrutsTypeConverter;
-
import com.bean.Point;
-
public class PointConverter extends StrutsTypeConverter {
-
-
@Override
-
public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
-
-
Point point = new Point();
-
String[] values= arg1[0].split(",");
-
-
int x = Integer.parseInt( values[0].trim() );
-
int y = Integer.parseInt( values[1].trim() );
-
-
point.setX(x);
-
point.setY(y);
-
-
return point;
-
-
-
}
-
-
@Override
-
public String convertToString(Map arg0, Object arg1) {
-
-
Point point = (Point) arg1;
-
-
int x = point.getX();
-
int y = point.getY();
-
-
String result = "<x= "+x+" , y="+y+" >";
-
-
return result;
-
}
-
-
}
第八步:
使类型转化器和action中的对应point属性关联起来新建一个properties文件
这里有两种方法:
第一种是在com.converter包中新建一个PointAction-conversion.properties文件
代码如下:
-
point1=com.converter.PointConverter
-
point2=com.converter.PointConverter
-
point3=com.converter.PointConverter
第二种:是在src目录下直接新建一个文件 xwork-conversion.properties
代码如下
-
com.bean.Point=com.converter.PointConverter
okstruts中的类型转换(2),布布扣,bubuko.com
struts中的类型转换(2)
原文:http://blog.csdn.net/estelle_belle/article/details/31389899