struts入门编写简单配置
一、引入jar包
需要什么jar包就可以自己去中央产库的源位置里找,里面会有一串代码,直接复制过来(pom.xml),格式如下图:
源位置:https://mvnrepository.com/
二、配置web.xml文件
web.xml文件是启动项目是加载的第一个文件
<!-- 修改web.xml由2.3至3.0 -->
<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_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- 配置中文乱码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>com.meng.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<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>*.action</url-pattern>
</filter-mapping>
</web-app>
三、配置struts-hello.xml
<?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>
<package name="struts-hello" extends="struts-base">
<action name="/helloAction" method="execute" class="com.meng.action.HelloStruts"></action>
</package>
</struts>
四、在struts.xml中添加struts-hello.xml文件
<?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>
<include file="struts-hello.xml"></include>
</struts>
五、编写java代码
package com.meng.action;
/**
* 用于演示Action类
* @author zjjt
*/
public class HelloStruts {
public String execute() {
System.out.println("hello struts");
return null;
}
}