Struts2框架学习(一)——Struts2的概念及搭建

一、Struts2的概念

使用优势:1)自动封装参数

     2)参数校验

     3)结果的处理(转发|重定向)

     4)国际化

     5)显示等待页面

     6)防止表单重复提交

Struts2具有更加先进的架构以及思想

Struts2的历史:1>Struts1与Struts1的区别技术上没有关系

        2>struts2的前身是webwork框架

二、搭建Struts2框架

首先下载Struts2的开发包,官网:https://struts.apache.org/

1、创建一个web工程并导入jar包

2、创建一个jsp页面

在WebContent下创建hello.jsp:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <h1>hello world!</h1>
11 </body>
12 </html>

3、书写Action类

1 package cn.itheima.a_hello;
2 
3 public class HelloAction {
4     
5     public String hello(){
6         System.out.print("hello world!");
7         return "success";
8     }
9 }

4、书写src下的struts.xml

Action类写好之后,Struts2框架如何识别它就是一个Action呢,需要对Action类进行配置

先导入约束(window--->Preferences--->XML--->XML Catalog--->User Specified Entries窗口,点击Add按钮)

在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="hello" namespace="/hello" extends="struts-default">
 7         <action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello">
 8             <result name="success">/hello.jsp</result>
 9         </action>
10     </package>
11 </struts>

5、将struts2核心过滤器配置到web.xml

web层的框架特点就是基于前端控制器的模式,这个前端控制器由过滤器实现,所以需要配置Struts2的核心过滤器,这个过滤器的名称是StrutsPrepareAndExecuteFilter。

在web.xml中进行如下配置:

6、发布到服务器并测试

原文地址:https://www.cnblogs.com/cxq1126/p/8466298.html