類型轉換css
注:本文系做者在看了浪曦的風中葉老師的struts2視頻的我的總結,但願能幫助廣大struts2的初學者。
第一步:(這一步和其餘同樣,這裏從簡)
依舊是新建一個web project,命名爲struts2,導入struts2必須的包。在src目錄下新建struts.xml,修改web.xml文件。
第二步:
將index.jsp更名爲input.jsp(這個不是必須的,事實上也沒有必要,此處只是爲了便於稱呼)。Input.jap的代碼以下
html
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> web
<% apache
String path = request.getContextPath(); jsp
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; ide
%> ui
<%@ taglib prefix="s" uri="/struts-tags" %> this
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> spa
<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
ok
struts2下的helloworld(如何讓第一個stru ... | 輸入校驗(1)——struts2第三講
struts2發佈文件.rar (2.3 MB)
下載次數: 92
struts2開發文件.rar (3.2 MB)
下載次數: 123