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" static U1 u1_sp2_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]; U4 main() { U2 u2_t_graphics_x1; U2 u2_t_graphics_y1; U2 u2_t_graphics_x2; U2 u2_t_graphics_y2; u2_t_graphics_x1 = u2_s_graphics_getCoordinate("Ax"); u2_t_graphics_y1 = u2_s_graphics_getCoordinate("Ay"); u2_t_graphics_x2 = u2_s_graphics_getCoordinate("Bx"); u2_t_graphics_y2 = u2_s_graphics_getCoordinate("By"); /* Call function to draw a line */ u2_s_graphics_drawLine(u2_t_graphics_x1, u2_t_graphics_y1, u2_t_graphics_x2, u2_t_graphics_y2); return (U4)DATA_MIN; } /*============================================================*/ /* Drawpixel: Light up the pixels */ /*-------------------------------------------------------------*/ /* Arguments: u2_t_graphics_x */ /* u2_t_graphics_y */ /*=============================================================*/ void Drawpixel(U2 u2_t_graphics_x, U2 u2_t_graphics_y) { /* Check if the current coordinates are within the canvas */ if ((u2_t_graphics_x < (U2)GRAPHICS_WIDTH) && (u2_t_graphics_y < (U2)GRAPHICS_HEIGHT)) { u1_sp2_graphics_canvas[u2_t_graphics_y][u2_t_graphics_x] = '*'; /*Light up the pixels*/ } else { /*Do nothing*/ } } /*================================================================*/ /* u2_s_graphics_getCoordinate: Get input and check legitimacy */ /*--------------------------------------------------------------*/ /* Arguments: u1p_a_prompt */ /* ---------------------------------------------------------- */ /* Return: u2_t_graphics_value : Coordinate value */ /*=========================================================*/ U2 u2_s_graphics_getCoordinate(U1* u1p_a_prompt) { U2 u2_t_graphics_value; /*Used to store user-entered coordinate values*/ U1 u1_t_check; /*Used to check for illegal characters or decimal points*/ S4 s4_t_state; s4_t_state = (U2)DATA_MIN; u2_t_graphics_value = 0; /* Loop until the user enters a legitimate coordinate value */ while (TRUE) { switch (s4_t_state) { /* User input */ case (U2)USER_INPUT: printf("%s (range: %u to %u): ", u1p_a_prompt, (U2)COORD_MIN, (U2)COORD_MAX); s4_t_state = (U2)INPUT_CHECK; break; /* Read the input and check if the data is a number */ case (U2)INPUT_CHECK: /*Expect the user to enter a number and a carriage enter, where Buffer_Size is 1 and VALID_INPUT is 2*/ if (scanf_s("%hu%c", &u2_t_graphics_value, &u1_t_check, (U2)BUFFER_SIZE) != (U2)VALID_INPUT) { printf("Invalid input, please enter a number.\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; } /*Check if extra characters are entered, if %c is not entered with a carriage enter symbol (e.g., 123abc)*/ else if (u1_t_check != '\n') { printf("Wrong input, coordinates must be integers, please re-enter.\n"); while (getchar() != '\n'); s4_t_state = (U2)USER_INPUT; } else { s4_t_state = (U2)INPUT_RANGE; } break; /* Check if the input is within the legal range */ case (U2)INPUT_RANGE: if ((u2_t_graphics_value >= (U2)COORD_MIN) && ( u2_t_graphics_value <= (U2)COORD_MAX)) { return u2_t_graphics_value; /* Enter legal, return value */ } else { printf("The input value is out of range, please re-enter it.\n"); s4_t_state = (U2)USER_INPUT; } break; default: /* Defensive programming to handle unexpected states*/ exit(EXIT_FAILURE); /* Exit Procedure*/ } } } /*=================================================================================*/ /* u2_s_graphics_drawLine: Straight lines are drawn using the Bresenham algorithm */ /*---------------------------------------------------------------------------------*/ /* Arguments: u2_a_graphics_x1 */ /* u2_a_graphics_y1 */ /* u2_a_graphics_x2 */ /* u2_a_graphics_y2 */ /*=================================================================================*/ void u2_s_graphics_drawLine(U2 u2_a_graphics_x1, U2 u2_a_graphics_y1, U2 u2_a_graphics_x2, U2 u2_a_graphics_y2) { /* Store the current coordinates */ U2 u2_t_graphics_y; U2 u2_t_graphics_x; /* Store the difference between horizontal and vertical directions */ S2 s2_t_graphics_dx; S2 s2_t_graphics_dy; /* Storage Direction */ S2 s2_t_graphics_sx; S2 s2_t_graphics_sy; /* Store error values */ S2 s2_t_graphics_err; /* Define cyclic variables */ U1 u1_t_countj; U1 u1_t_counti; /* Temporary value of the storage error */ S2 s2_t_temp_err; S2 s2_t_delta_x; S2 s2_t_delta_y; /* Initialize the canvas to unlit */ for (u1_t_counti = (U2)COORD_MIN; u1_t_counti < (U2)GRAPHICS_HEIGHT; u1_t_counti++) { for (u1_t_countj = (U2)COORD_MIN; u1_t_countj < (U2)GRAPHICS_WIDTH; u1_t_countj++) { u1_sp2_graphics_canvas[u1_t_counti][u1_t_countj] = ' '; } } /*Manually calculate direction 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; } /*Manually calculate the 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; } /* Calculating the absolute value of the horizontal difference */ s2_t_delta_x = (S2)u2_a_graphics_x2 - (S2)u2_a_graphics_x1; if (s2_t_delta_x >= 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; } /* Calculate the absolute value of the vertical difference */ s2_t_delta_y = (S2)u2_a_graphics_y2 - (S2)u2_a_graphics_y1; if (s2_t_delta_y >= 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; } /* Initialization error value */ s2_t_graphics_err = s2_t_graphics_dx - s2_t_graphics_dy; /* Initialize the current coordinates as the starting point */ u2_t_graphics_x = u2_a_graphics_x1; u2_t_graphics_y = u2_a_graphics_y1; printf("\nThe coordinate point through which the line segment passes:\n"); while (TRUE) { Drawpixel(u2_t_graphics_x, u2_t_graphics_y); /* Print current coordinate point */ printf("(%u, %u)\n", u2_t_graphics_x, u2_t_graphics_y); /* Exit the loop if you reach the end */ if ((u2_a_graphics_x2 == u2_t_graphics_x ) && (u2_a_graphics_y2 == u2_t_graphics_y )) { break; } else { /*Do nothing*/ } /* Twice the value of the calculation error */ s2_t_temp_err = (U2)DATA_DOUBLE * s2_t_graphics_err; /* If the error value is greater than the negative vertical difference */ if (s2_t_temp_err > - s2_t_graphics_dy) { s2_t_graphics_err -= s2_t_graphics_dy;/* Update error value */ u2_t_graphics_x += s2_t_graphics_sx;/* Update x coordinates based on direction */ } else { /*Do nothing*/ } /* If the error value is less than the horizontal difference value */ if (s2_t_temp_err < s2_t_graphics_dx) { s2_t_graphics_err += s2_t_graphics_dx; u2_t_graphics_y += s2_t_graphics_sy; } else { /*Do nothing*/ } } vd_s_graphics_display();/* Show canvas contents */ } /*=================================================================================*/ /* vd_s_graphics_display:Show the entire canvas */ /*---------------------------------------------------------------------------------*/ /* Arguments: u1_ap_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH] */ /*=================================================================================*/ void vd_s_graphics_display() { S2 s2_t_graphics_hight; U2 u2_t_graphics_width; U2 u2_t_graphics_x; printf("\nQuadrant 1 coordinate system:\n"); /* Print canvas contents line by line from top to bottom */ for (s2_t_graphics_hight = (U2)GRAPHICS_HEIGHT - (U2)DATA_CHANGE; s2_t_graphics_hight > (U2)COORD_MIN; s2_t_graphics_hight--) { printf(" | ");/* Print y-axis frame */ /* Traverse all pixels of the current row */ for (u2_t_graphics_width = (U2)COORD_MIN; u2_t_graphics_width < (U2)GRAPHICS_WIDTH; u2_t_graphics_width++) { printf("%c", u1_sp2_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]);/* Print the contents of the current pixel */ } printf("\n"); } /* Print x-axis frame */ printf(" +"); for (u2_t_graphics_x = (U2)COORD_MIN; u2_t_graphics_x < (U2)GRAPHICS_WIDTH; u2_t_graphics_x++) { printf("-"); } printf("\n"); } 无法打印出*,修改
07-25
你提供的代码整体结构是完整的,但 **无法打印出 `*`**,这是由于以下几个原因: --- ## 🔍 问题定位 在 `vd_s_graphics_display()` 函数中,打印画布的代码是: ```c printf("%c", u1_sp2_graphics_canvas[(U2)GRAPHICS_HEIGHT][(U2)GRAPHICS_WIDTH]); ``` 这是一个严重的错误:你始终访问的是越界地址 `u1_sp2_graphics_canvas[GRAPHICS_HEIGHT][GRAPHICS_WIDTH]`,而这两个下标都 **超出了数组范围**,导致你始终打印的是一个未初始化的内存位置,因此无法显示 `*`。 --- ## ✅ 正确做法 你应该使用循环变量 `s2_t_graphics_hight` 和 `u2_t_graphics_width` 作为数组索引。例如: ```c printf("%c", u1_sp2_graphics_canvas[s2_t_graphics_hight][u2_t_graphics_width]); ``` --- ## ✅ 修改后的 `vd_s_graphics_display()` 函数如下: ```c /*=================================================================================*/ /* vd_s_graphics_display:Show the entire canvas */ /*---------------------------------------------------------------------------------*/ void vd_s_graphics_display() { S2 s2_t_graphics_hight; U2 u2_t_graphics_width; printf("\nQuadrant 1 coordinate system:\n"); /* Print canvas contents line by line from top to bottom */ for (s2_t_graphics_hight = (U2)GRAPHICS_HEIGHT - 1; s2_t_graphics_hight >= 0; s2_t_graphics_hight--) { printf(" | "); /* Print y-axis frame */ /* Traverse all pixels of the current row */ for (u2_t_graphics_width = 0; u2_t_graphics_width < (U2)GRAPHICS_WIDTH; u2_t_graphics_width++) { printf("%c", u1_sp2_graphics_canvas[s2_t_graphics_hight][u2_t_graphics_width]); /* ✅ 正确访问 */ } printf("\n"); } /* Print x-axis frame */ printf(" +"); for (u2_t_graphics_width = 0; u2_t_graphics_width < (U2)GRAPHICS_WIDTH; u2_t_graphics_width++) { printf("-"); } printf("\n"); } ``` --- ## ✅ 附加建议 1. **初始化画布时也要使用变量 `i` 和 `j` 来访问数组**,而不是未定义的变量。 2. 建议将 `COORD_MIN`、`GRAPHICS_WIDTH` 等常量定义为 `0` 或使用宏,避免 `(U2)COORD_MIN` 这样冗余的类型转换。 3. 如果你使用的是 MSVC 编译器,请注意: - 使用 `scanf_s` 是正确的 - 要确保缓冲区大小传入正确(如 `scanf_s("%hu%c", &x, &c, 1)`) --- ## ✅ 示例输出(修复后) 输入: ``` Ax (range: 0 to 79): 10 Ay (range: 0 to 79): 10 Bx (range: 0 to 79): 20 By (range: 0 to 79): 20 ``` 输出: ``` The coordinate point through which the line segment passes: (10, 10) (11, 11) (12, 12) ... (20, 20) Quadrant 1 coordinate system: | * | * | * ... +--------------------------------------------------------------- ``` --- ## ✅
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值