struts2


第一步:(这一步和其他一样,这里从简)
依旧是新建一个web project,命名为struts2,导入struts2必须的包。在src目录下新建struts.xml,修改web.xml文件。

第二步:
将index.jsp改名为input.jsp(这个不是必须的,事实上也没有必要,此处只是为了便于称呼)。Input.jap的代码如下
Java代码 收藏代码
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
  2. <%
  3. Stringpath=request.getContextPath();
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <%@taglibprefix="s"uri="/struts-tags"%>
  7. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  8. <html>
  9. <head>
  10. <basehref="<%=basePath%>">
  11. <title>MyJSP'index.jsp'startingpage</title>
  12. <metahttp-equiv="pragma"content="no-cache">
  13. <metahttp-equiv="cache-control"content="no-cache">
  14. <metahttp-equiv="expires"content="0">
  15. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  16. <metahttp-equiv="description"content="Thisismypage">
  17. <!--
  18. <linkrel="stylesheet"type="text/css"href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. <h1><fontcolor='red'>请输入坐标,用英文半角逗号隔开</font></h1>
  23. <s:formaction="pointconverter">
  24. <s:textfieldname="point1"label="point1"></s:textfield>
  25. <s:textfieldname="point2"label="point2"></s:textfield>
  26. <s:textfieldname="point3"label="point3"></s:textfield>
  27. <s:submitname="submit"></s:submit>
  28. </s:form>
  29. </body>
  30. </html>


该文件有两个要注意的地方
1.使用了struts2的标签库 <%@ taglib prefix="s" uri="/struts-tags" %>
2.f注意form中的action属性

第三步:
在src下新建包com.beam,其中定义point类 point.java 代码如下:
Java代码 收藏代码
  1. packagecom.bean;
  2. publicclassPoint{
  3. publicintgetX(){
  4. returnx;
  5. }
  6. publicvoidsetX(intx){
  7. this.x=x;
  8. }
  9. publicintgetY(){
  10. returny;
  11. }
  12. publicvoidsetY(inty){
  13. this.y=y;
  14. }
  15. privateintx;
  16. privateinty;
  17. }


Action
在src下新建包com.action
其中新建类PointAction.java 代码如下
Java代码 收藏代码
  1. packagecom.action;
  2. importcom.opensymphony.xwork2.ActionSupport;
  3. importcom.bean.Point;
  4. publicclassPointActionextendsActionSupport
  5. {
  6. publicPointgetPoint1(){
  7. returnpoint1;
  8. }
  9. publicvoidsetPoint1(Pointpoint1){
  10. this.point1=point1;
  11. }
  12. publicPointgetPoint2(){
  13. returnpoint2;
  14. }
  15. publicvoidsetPoint2(Pointpoint2){
  16. this.point2=point2;
  17. }
  18. publicPointgetPoint3(){
  19. returnpoint3;
  20. }
  21. publicvoidsetPoint3(Pointpoint3){
  22. this.point3=point3;
  23. }
  24. publicStringexecute()throwsException
  25. {
  26. returnSUCCESS;
  27. }
  28. privatePointpoint1;
  29. privatePointpoint2;
  30. privatePointpoint3;
  31. }


第五步:配置struts.xml文件 代码如下:
Xml代码 收藏代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <!DOCTYPEstrutsPUBLIC
  3. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  4. "struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6. <packagename="struts2"extends="struts-default">
  7. <actionname="pointconverter"class="com.action.PointAction">
  8. <resultname="success">/output.jsp</result>
  9. <resultname="input">/input.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

第六步:
在WebRoot下新建视图output.jsp 依旧运用struts2的标签库 代码如下
Jsp代码 收藏代码
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="GB18030"%>
  2. <%
  3. Stringpath=request.getContextPath();
  4. StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <%@taglibprefix="s"uri="/struts-tags"%>
  7. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  8. <html>
  9. <head>
  10. <basehref="<%=basePath%>">
  11. <title>MyJSP'output.jsp'startingpage</title>
  12. <metahttp-equiv="pragma"content="no-cache">
  13. <metahttp-equiv="cache-control"content="no-cache">
  14. <metahttp-equiv="expires"content="0">
  15. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  16. <metahttp-equiv="description"content="Thisismypage">
  17. <!--
  18. <linkrel="stylesheet"type="text/css"href="styles.css">
  19. -->
  20. </head>
  21. <body>
  22. point1:<s:propertyvalue="point1"/><br>
  23. point2:<s:propertyvalue="point2"/><br>
  24. point3<s:propertyvalue="point3"/>
  25. </body>
  26. </html>

第七步:类型转化器
在src目录下新建com.converter包 其中新建类PointConverter.java 代码如下
Java代码 收藏代码
  1. packagecom.converter;
  2. importjava.util.Map;
  3. importorg.apache.struts2.util.StrutsTypeConverter;
  4. importcom.bean.Point;
  5. publicclassPointConverterextendsStrutsTypeConverter{
  6. @Override
  7. publicObjectconvertFromString(Maparg0,String[]arg1,Classarg2){
  8. Pointpoint=newPoint();
  9. String[]values=arg1[0].split(",");
  10. intx=Integer.parseInt(values[0].trim());
  11. inty=Integer.parseInt(values[1].trim());
  12. point.setX(x);
  13. point.setY(y);
  14. returnpoint;
  15. }
  16. @Override
  17. publicStringconvertToString(Maparg0,Objectarg1){
  18. Pointpoint=(Point)arg1;
  19. intx=point.getX();
  20. inty=point.getY();
  21. Stringresult="<x="+x+",y="+y+">";
  22. returnresult;
  23. }
  24. }


第八步:
使类型转化器和action中的对应point属性关联起来新建一个properties文件
这里有两种方法:
第一种是在com.converter包中新建一个PointAction-conversion.properties文件
代码如下:
Properties代码 收藏代码
  1. point1=com.converter.PointConverter
  2. point2=com.converter.PointConverter
  3. point3=com.converter.PointConverter


第二种:是在src目录下直接新建一个文件 xwork-conversion.properties
代码如下
Properties代码 收藏代码
  1. com.bean.Point=com.converter.PointConverter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值