Eclipse配置Struts2

使用工具

编程工具:Ecilpse
JDK版本:1.8
Tomcat版本:9.0
Struct版本:2.5.20

一、前期准备

配置JDK、下载tomcat我就不说了
弄好后cmd测试 javac -version


显示JDK版本,这里JDK就配置好了
Tomcat下载好后在bin文件夹中点击startup文件启动一下 注:此处启动窗口不能关哈
然后http://localhost:8080/查看下

在这里插入图片描述
显示这个界面后服务器也测试成功了哈
OK 现在我们来下载一下Struts2的相关文件。
直接搜索structs
在这里插入图片描述
进入后
在这里插入图片描述
点击下载
在这里插入图片描述初学者我们直接点击全部下载,下载好了后解压得到struts2.5.20的文件夹,OK这面前期准备就到这,现在打开我们的Ecilipse创建动态web项目

二、创建web项目

撒
若是新创建的工作空间,直接在创建过程中选择好服务器和jdk,创建web项目后,起个名字,不要着急finish,next看一下有个位置是生成web.xml的记得勾起,之后确定,此时项目结构如下
在这里插入图片描述
OK,接下来我们开始着手导入struts的jar包
在我们刚刚下载好的struts文件夹下进入lib目录下
将以下的jar包复制到我们项目中的WEB-INF下的lib中
在这里插入图片描述
全导入会报错哈,我们还是将这些包选出来直接复制进lib中就好

导入过后,配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>struts01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

注意filter-class 最新版本的dispatcher后没有 .ng 许多书籍中使用的struts都是老版本,这个位置一定看仔细,过滤器这里配置不成功,整个struts框架都不能加载成功
写三个jsp页面

在这里插入图片描述
index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello World Struts2</h1>
<form action="hello.action">
<label for="name">Please enter your name</label><br>
<input type="text" name="name"/>
<input type="submit" value="Enter"/>
</form>
</body>
</html>

HelloWorld.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 Hello World,Welcome <s:property value="name"/> 
</body>
</html>

Error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
You did not have entered a name!
</body>
</html>

创建HelloWorldAction.java
在这里插入图片描述
配置struts.xml,在src目录下创建

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true"/>
 <package name="com.action" extends="struts-default">
  <action name="hello" class="com.action.HelloWorldAction" method="execute">
   <result name="success">/HelloWorld.jsp</result>
   <result name="error">/Error.jsp</result>
  </action>
 </package>
</struts>   

OK 运行index.jsp
在这里插入图片描述

在这里插入图片描述

编程内容转自:https://blog.csdn.net/qq_17058993/article/details/86627269

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值