java之struts框架入门教程

布满荆棘的人生 2021-11-01 09:32 514阅读 0赞

本教程主要讲述struts的简单入门操作

使用的是myeclipse工具

1.创建web项目

1182288-20190514225700558-1713055154.png

1182288-20190514225712528-124663100.png

2.复制struts必要的jar包到 WebRoot/WEB-INF/lib 下

jar包列表如下:

  1. asm-3.3.jar
  2. asm-commons-3.3.jar
  3. asm-tree-3.3.jar
  4. commons-fileupload-1.2.2.jar
  5. commons-io-2.0.1.jar
  6. commons-lang3-3.1.jar
  7. freemarker-2.3.19.jar
  8. javassist-3.11.0.GA.jar
  9. ognl-3.0.5.jar
  10. struts2-core-2.3.4.jar
  11. xwork-core-2.3.4.jar

导入后的项目结构如图

1182288-20190514230214765-2120524222.png

3.在 WebRoot/WEB-INF 下添加 web.xml

内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  3. <display-name>01struts2_helloworld</display-name>
  4. <!-- struts2的核心过滤器配置 -->
  5. <filter>
  6. <filter-name>struts2</filter-name>
  7. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  8. </filter>
  9. <filter-mapping>
  10. <filter-name>struts2</filter-name>
  11. <url-pattern>*.action</url-pattern>
  12. </filter-mapping>
  13. <welcome-file-list>
  14. <welcome-file>index.html</welcome-file>
  15. <welcome-file>index.htm</welcome-file>
  16. <welcome-file>index.jsp</welcome-file>
  17. <welcome-file>default.html</welcome-file>
  18. <welcome-file>default.htm</welcome-file>
  19. <welcome-file>default.jsp</welcome-file>
  20. </welcome-file-list>
  21. </web-app>

目录结构如下:

1182288-20190514230627727-1264444201.png

4.在src下添加java类来接收请求

  1. public class HelloAction {
  2. struts2的处理方法 都是 public String 默认执行execute,并且处理方法没有参数
  3. public String execute(){
  4. System.out.println("请求被接收了...");
  5. return "success";
  6. }
  7. }

5.在src下,添加struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
  4. "http://struts.apache.org/dtds/struts-2.3.dtd">
  5. <struts>
  6. <package name="default" extends="struts-default" namespace="/">
  7. <action name="hello" class="cn.qm.action.HelloAction">
  8. <result name="success">/index.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

注意:文件名需要是 struts.xml ,如果文件名拼写错误,

或者是struts.xml文件的路径错误,或者struts.xml内容有错误,

后面请求时都会报 “There is no Action mapped for namespace / and action name” 错误

目录结构如下图:

1182288-20190514231953573-1942117575.png

6.修改请求时项目的映射路径

点击项目右键,选择 Properties

1182288-20190514232236186-59193704.png

输入web,找到Context Root ,并且修改Web Context-root 为 /Hello ,如图

1182288-20190514232319472-20463110.png

7.配置项目到服务器

在Servers下,Tomcat 8.x 上右键 Add Deployment

1182288-20190514232701288-139990786.png

选择HelloStruts项目

1182288-20190514232828165-503263325.png

效果图

1182288-20190514232907803-1406651622.png

8.点击上图绿色三角,运行

9.在浏览器输入 http://localhost:8080/Hello/hello.action 检测是否接收到请求

效果如下

myeclipse的console中显示如下:

1182288-20190514233337583-1129754749.png

浏览器效果

1182288-20190514233436539-1827303548.png

10.为了是浏览器显示效果更明显,可以把This is my JSP page 替换为 hello struts

index.jsp修改如下

注意:pageEncoding 修改为 utf-8 的编码方式

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>My JSP 'index.jsp' starting page</title>
  11. <meta http-equiv="pragma" content="no-cache">
  12. <meta http-equiv="cache-control" content="no-cache">
  13. <meta http-equiv="expires" content="0">
  14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  15. <meta http-equiv="description" content="This is my page">
  16. <!--
  17. <link rel="stylesheet" type="text/css" href="styles.css">
  18. -->
  19. </head>
  20. <body>
  21. hello struts ... <br>
  22. </body>
  23. </html>

  

效果图:

1182288-20190514233838500-630795608.png

ok,本教程结束

因为参考的是之前的一些老的资料,所以操作方式相比现在回相对落后,

但是,这里只是为了帮助自己记录知识,以及回忆操作

后面如果有机会,会使用相对较新的方式来创建struts

这里附上 struts官网教程

大致看了一下,这里用到了maven进行jar包管理,

并且其中对于jar使用方便了很多

转载于:https://www.cnblogs.com/Vincent-yuan/p/10865416.html

发表评论

表情:
评论列表 (有 0 条评论,514人围观)

还没有评论,来说两句吧...

相关阅读