s2s2

本文介绍如何在Eclipse中创建Dynamic Web Application,并实现Struts2与Spring框架的集成。主要步骤包括配置必要的库文件、修改web.xml文件、编写HelloWorld Action、配置struts.xml和applicationContext.xml文件。
在Eclipse下建立一个Dynamic Web Application。

从struts2.0.6的lib目录中复制下面的库文件到WEB-INF/lib目录下:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.9.jar
struts-api-2.0.6.jar
struts-core-2.0.6.jar
struts-spring-plugin-2.0.6.jar
xwork-2.0.0.jar

从spring中lib目录中复制下面的库文件到WEB-INF/lib目录下:
spring.jar

修改web.xml,增加一个struts的分派器filter,映射所有的url-pattern,再增加一个spring的ContextLoaderListener监听器。修改后的内容如下:
xml 代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <web-appid="WebApp_ID"version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <display-name>struts2tutorial</display-name>
  7. <filter>
  8. <filter-name>struts2</filter-name>
  9. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  10. </filter>
  11. <filter-mapping>
  12. <filter-name>struts2</filter-name>
  13. <url-pattern>*.action</url-pattern>
  14. </filter-mapping>
  15. <welcome-file-list>
  16. <welcome-file>index.jsp</welcome-file>
  17. </welcome-file-list>
  18. <listener>
  19. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  20. </listener>
  21. </web-app>


写一个简单的Action,HelloWorld:
java 代码
  1. packagetutorial;
  2. importcom.opensymphony.xwork2.ActionSupport;
  3. publicclassHelloWorldextendsActionSupport{
  4. publicstaticfinalStringMESSAGE="Strutsisupandrunning...";
  5. publicStringexecute()throwsException{
  6. setMessage(MESSAGE);
  7. returnSUCCESS;
  8. }
  9. privateStringmessage;
  10. publicvoidsetMessage(Stringmessage){
  11. this.message=message;
  12. }
  13. publicStringgetMessage(){
  14. returnmessage;
  15. }
  16. }


在源文件路径下(项目的src目录)增加struts.xml配置action。这个文件是集成spring的关键所在,这里面描述有如何将spring2集成到struts2的相关信息:
xml 代码
  1. <!DOCTYPEstrutsPUBLIC
  2. "-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
  3. "http://struts.apache.org/dtds/struts-2.0.dtd">
  4. <struts>
  5. <!-- 下面这句表明对象是由spring负责产生的.加上这句后,struts会产生让spring负责
  6. 产生bean,如果spring不能产生bean,则由struts自己产生.也可以在struts.properties
  7. 文件内定义这个属性.-->
  8. <constantname="objectFactory"value="spring"></constant>
  9. <packagename="struts2tutoial"extends="struts-default"namespace="/">
  10. <!-- 注意,现在action的class属性不再是类的名字了,而是在spring中的bean的id
  11. 详细信息请看下面的spring的bean配置文件applicationContext.xml-->
  12. <actionname="HelloWorld"class="helloWorld">
  13. <result>/helloWorld.jsp</result>
  14. </action>
  15. <!--Addyouractionshere-->
  16. </package>
  17. </struts>


在WEB-INF/目录下增加spring的bean配置文件applicationContext.xml:
xml 代码
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN2.0//EN""http://www.springframework.org/dtd/spring-beans-2.0.dtd">
  3. <beans>
  4. <beanid="helloWorld"class="tutorial.HelloWorld"></bean>
  5. </beans>

配置很简单,只有一个bean。

最后,在WebContent目录下增加helloWorld.jsp:
xml 代码
  1. <%@taglibprefix="s"uri="/struts-tags"%>
  2. <html>
  3. <head>
  4. <title>HelloWorld!</title>
  5. </head>
  6. <body>
  7. <h2><s:propertyvalue="message"/></h2>
  8. </body>
  9. </html>

#include <stdio.h> #include <stdlib.h> #include "aip_common.h" #include "Num1.h" U4 main() { // 定义变量,遵循命名规则 U2 u2_t_graphics_x1; U2 u2_t_graphics_y1; U2 u2_t_graphics_x2; U2 u2_t_graphics_y2; // 提示用户输入A点的坐标 u2_t_graphics_x1 = u2_s_graphics_getCoordinate("请输入A点的x坐标"); u2_t_graphics_y1 = u2_s_graphics_getCoordinate("请输入A点的y坐标"); // 提示用户输入B点的坐标 u2_t_graphics_x2 = u2_s_graphics_getCoordinate("请输入B点的x坐标"); u2_t_graphics_y2 = u2_s_graphics_getCoordinate("请输入B点的y坐标"); u2_s_graphics_drawLine(u2_t_graphics_x1, u2_t_graphics_y1, u2_t_graphics_x2, u2_t_graphics_y2);/* 调用函数绘制直线 */ return (U2)DATA_MIN; } /*=========================================================*/ /* Quit Sort Function : Recursive Quick Sort Algorithm.*/ /*-------------------------------------------------------------------------*/ /* Arguments: U4 u4_ap_array_data[] : Array to be sorted*/ /* U4 u4_a_postion_left : Left boundary of the array segment*/ /* U4 u4_a_postion_right : Right boundary of the array segment*/ /*=========================================================*/ U2 u2_s_graphics_getCoordinate( U1* u1p_a_prompt) { /* 获取用户输入的坐标,并检查合法性 */ U2 u2_t_graphics_value; /*用于存储用户输入的坐标值*/ U1 u1_t_check; /*用于检查非法字符或小数点*/ S4 s4_t_state; /* 定义状态变量,控制输入流程 */ s4_t_state = (U2)DATA_MIN; while (TRUE) { /* 循环直到用户输入合法的坐标值 */ switch (s4_t_state) { /* 用户输入 */ case (U2)USER_INPUT: printf("%s (范围: %u 到 %u): ", u1p_a_prompt, (U2)COORD_MIN, (U2)COORD_MAX); s4_t_state = (U2)INPUT_CHECK; /* 转到读取输入状态 */ break; /* 读取输入并检查数据是否是数字 */ case (U2)INPUT_CHECK: /*期待用户输入一个数字和一个回车,其中Buffer_Size是 1,VALID_INPUT是2*/ if (scanf_s("%hu%c", &u2_t_graphics_value, &u1_t_check, (U2)BUFFER_SIZE) != (U2)VALID_INPUT ) { printf("输入无效,请输入一个数字。\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; }/*检查是否输入了多余字符,若%c输入的不是回车符号(例如:123abc)*/ else if (u1_t_check != '\n') { printf("输入错误,坐标必须是整数,请重新输入。\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; } else { s4_t_state = (U2)INPUT_RANGE; } break; /* 检查输入是否在合法范围内 */ case (U2)INPUT_RANGE: if (u2_t_graphics_value >= (U2)COORD_MIN && u2_t_graphics_value <= (U2)COORD_MAX) { return u2_t_graphics_value; /* 输入合法,返回值*/ } else { printf("输入值超出范围,请重新输入。\n"); s4_t_state = (U2)USER_INPUT; } break; default: /* 防御性编程,处理意外状态*/ exit(EXIT_FAILURE); // 退出程序 } } } /* 使用Bresenham算法绘制直线 */ U2 u2_s_graphics_drawLine(U2 u2_a_graphics_x1, U2 u2_a_graphics_y1, U2 u2_a_graphics_x2, U2 u2_a_graphics_y2) { /* 定义变量,用于存储当前坐标 */ U2 u2_t_graphics_y; U2 u2_t_graphics_x; /* 定义变量,存储水平和垂直方向的差值 */ S2 s2_t_graphics_dx; S2 s2_t_graphics_dy; /* 定义变量,存储方向 */ S2 s2_t_graphics_sx; S2 s2_t_graphics_sy; /* 定义变量,存储误差值 */ S2 s2_t_graphics_err; /* 定义循环变量 */ U1 u1_t_countj; U1 u1_t_counti; /* 定义变量,存储误差的临时值 */ S2 s2_t_graphics_e2; /* 定义画布,初始化为空格 */ U1 u1_tp_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]; /* 初始化画布为未点亮状态 */ for ( u1_t_counti = (U2)DATA_MIN; u1_t_counti < (U2)GRAPHICS_HEIGHT; u1_t_counti++) { for (u1_t_countj = (U2)DATA_MIN; u1_t_countj < (U2)GRAPHICS_WIDTH; u1_t_countj++) { u1_tp_graphics_canvas[u1_t_counti][u1_t_countj] = ' '; } } /*手动计算方向 x*/ if (u2_a_graphics_x1 < u2_a_graphics_x2) { s2_t_graphics_sx = (S2)POSITIVE_DIRECTION; } else { s2_t_graphics_sx = (S2)NEGATIVE_DIRECTION; } /*手动计算方向 y*/ if (u2_a_graphics_y1 < u2_a_graphics_y2) { s2_t_graphics_sy = (S2)POSITIVE_DIRECTION; } else { s2_t_graphics_sy = (S2)NEGATIVE_DIRECTION; } /* 计算水平差值的绝对值 */ if (((S2)u2_a_graphics_x2 - (S2)u2_a_graphics_x1) >= 0) { s2_t_graphics_dx = (S2)u2_a_graphics_x2 - (S2)u2_a_graphics_x1; } else { s2_t_graphics_dx = (S2)u2_a_graphics_x1 - (S2)u2_a_graphics_x2; } /* 计算垂直差值的绝对值 */ if (((S2)u2_a_graphics_y2 - (S2)u2_a_graphics_y1) >= 0) { s2_t_graphics_dy = (S2)u2_a_graphics_y2 - (S2)u2_a_graphics_y1; } else { s2_t_graphics_dy = (S2)u2_a_graphics_y1 - (S2)u2_a_graphics_y2; } /* 初始化误差值 */ s2_t_graphics_err = s2_t_graphics_dx - s2_t_graphics_dy; /* 初始化当前坐标为起点 */ u2_t_graphics_x = u2_a_graphics_x1; u2_t_graphics_y = u2_a_graphics_y1; printf("\n线段经过的坐标点:\n"); while (TRUE) { /* 检查当前坐标是否在画布范围内 */ if (u2_t_graphics_x < (U2)GRAPHICS_WIDTH && u2_t_graphics_y < (U2)GRAPHICS_HEIGHT) { u1_tp_graphics_canvas[u2_t_graphics_y][u2_t_graphics_x] = '*'; // 点亮像素 } /* 打印当前坐标点 */ printf("(%u, %u)\n", u2_t_graphics_x, u2_t_graphics_y); /* 如果到达终点,退出循环 */ if (u2_t_graphics_x == u2_a_graphics_x2 && u2_t_graphics_y == u2_a_graphics_y2) { break; } /* 计算误差的两倍值 */ s2_t_graphics_e2 = (U2)DATA_DOUBLE * s2_t_graphics_err; /* 如果误差值大于负的垂直差值 */ if (s2_t_graphics_e2 > -s2_t_graphics_dy) { s2_t_graphics_err -= s2_t_graphics_dy;/* 更新误差值 */ u2_t_graphics_x += s2_t_graphics_sx;/* 根据方向更新x坐标 */ } else { /*Do nothing*/ } /* 如果误差值小于水平差值 */ if (s2_t_graphics_e2 < s2_t_graphics_dx) { s2_t_graphics_err += s2_t_graphics_dx;/* 更新误差值 */ u2_t_graphics_y += s2_t_graphics_sy;/* 根据方向更新y坐标 */ } else { /*Do nothing*/ } } vd_s_graphics_display(u1_tp_graphics_canvas);/* 显示画布内容 */ } // 显示整个画布 void vd_s_graphics_display( U1 u1_ap_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]) { S2 s2_t_graphics_hight;/*画布高度*/ U2 u2_t_graphics_width;/*画布宽度*/ U2 u2_t_graphics_x; /*x 轴坐标*/ printf("\n第一象限坐标系:\n"); /* 从顶部到底部逐行打印画布内容 */ for ( s2_t_graphics_hight = (U2)GRAPHICS_HEIGHT - (U2)DATA_CHANGE; s2_t_graphics_hight > (U2)DATA_MIN; s2_t_graphics_hight--) { printf(" | ");/* 打印y轴框架 */ /* 遍历当前行的所有像素点 */ for (u2_t_graphics_width = (U2)DATA_MIN; u2_t_graphics_width < (U2)GRAPHICS_WIDTH; u2_t_graphics_width++) { printf("%c", u1_ap_graphics_canvas[s2_t_graphics_hight][u2_t_graphics_width]);/* 打印当前像素点的内容 */ } printf("\n"); } /* 打印x轴框架 */ printf(" +"); for ( u2_t_graphics_x = (U2)DATA_MIN; u2_t_graphics_x < (U2)GRAPHICS_WIDTH; u2_t_graphics_x++) { printf("-");/* 打印x轴的横线 */ } printf("\n"); } 把打印像素的函数单独用Drawpixel(unsigned short x,unsigned short y);写出啦
07-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值