第一个Struts2程序之HelloWorld

时间:2023-03-09 03:11:53
第一个Struts2程序之HelloWorld

1、Struts2 简介

Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。

2、Struts2实例之HelloWorld

2.1 web.xml配置代码

 <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.2 struts.xml配置代码

 <struts>
<package name="helloWorld" extends="struts-default">
<action name="hello" class="com.action.HelloWorldAction">
<result name="success">helloWorld.jsp</result>
</action>
</package>
</struts>

2.3 HelloWorldAction代码

 public class HelloWorldAction implements Action{

     @Override
public String execute() throws Exception {
System.out.println("执行了Action默认方法!!");
return SUCCESS;
}
}

2.4 helloWorld.jsp代码

 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Struts2</title>
</head>
<body>
Struts2大爷你好!
</body>
</html>

  每天进步一点点,勿忘初心,一切都是最好的安排。