环境需要
- comcat8.5.73
- Maven3.8.4
- jdk1.8
- 新建项目
注意把对钩打上 create from archeType。
选择Maven的文件位置
修改pom.xml
插入一下代码
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.29</version>
</dependency>
重构项目
新建文件:参考目录
新建 web.xml
替换为以下代码
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
新建struts.xml 替换为以下代码
这里配置了扩展名.do
<?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.devMode" value="true"/>
<!-- 打开动态方法-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<!-- 修改struts2的action的扩展名为.do-->
<constant name="struts.action.extension" value="do"/>
<package name="test" extends="struts-default">
<action name="first" class="org.example.FirstAction">
<result>/first.jsp</result>
</action>
</package>
</struts>
点击org.example右键新建
在org.example包中新建FirstAction.java
package org.example;
/**
* @author pig
*/
public class FirstAction {
public String execute() {
return "success";
}
}
在webapp中新建first.jsp文件
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>first.jsp</title>
</head>
<body>
first------------------------------
</body>
</html>
添加tomcat服务器
启动项目 debug
看到helloWorld!
浏览器地址栏输入localhost:9999/first.do
遇到问题 欢迎评论
加油陌生人