1 什么是Struts2框架

  基于MVC设计模式的web应用框架

  Struts2框架是一个轻量级的MVC流程框架

    轻量级是指程序的代码不是很多,运行时占用的资源不是很多,MVC流程框架就是说它是支持分层开发,控制数据的流程,从哪里来,到那里去,怎么来,怎么去的这样一个框架;  

  Struts1 和 Struts2 没有任何关系
  Struts2的前身是WebWork
  Struts1/Struts2/SpringMVC都是MVC设计模式的表现层框架【SpringMVC现在最流行】

2 如何使用Struts2框架

  2.1 导包 Struts2-core

  2.2 配置主控制器,在web.xml中进行配置(注意:这里的主控制器相当于SpringMVC的前端控制器)

  2.3 配置struts.xml配置文件(注意:这里的配置文件名不能进行更改)

3 案例:使用Struts2表现框架实现一个helloworld案例

  3.1 案例效果

    在浏览器输入:http://localhost:8080/ssh01/demo/hello

    服务器返回的结果是:

      

  3.2 配置主控制器    

    

      注意:主控制器类在maven中的位置

        

  3.3 配置struts.xml配置文件

    3.3.1 手动创建一个struts.xml文件

      》创建一个xml文件,文件名为struts

      》打开默认的struts.xml文件

        

      》将默认struts.xml文件中24——26中内容复制到自己创建的struts.xml文件中

        

      》在自己创建的struts.xml文件中添加一个<struts></struts>标签

    3.3.2 配置struts.xml文件

      》配置请求路径

        通过 package 标签实现

          name属性:随便写个名字就行,无什么实际意义

          namespace属性:请求路径

          extends属性:继承默认的struts.xml配置文件

      》配置具体请求名

        通过 action 标签实现

          name属性:具体的请求名(注意:会默认在后面添加 .action ,所以访问时使用 hello 和 hello.action 都可以)

          class属性:请求控制类

      》配置返回的结果

        通过 result 标签实现

          name属性:该属性值必须和控制类中execute方法的返回值保持一致

       

  3.4 编写控制类

    该类中必须有一个execute方法,该方法是用来处理请求的

    

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiangxu</groupId>
<artifactId>ssh01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.8</version>
</dependency>
</dependencies>
</project>

pom.xml (maven依赖)

 <?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd" version="2.5">
<display-name>ssh01</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> <!-- 配置主控制器
配置方法和selvet的配置相似
相当于SpringMVC框架的前端控制器 -->
<filter>
<filter-name>StrutsMVC</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>StrutsMVC</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> </web-app>

web.xml (主控制器配置)

 <?xml version="1.0" encoding="UTF-8"?>

 <!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="test" namespace="/demo" extends="struts-default">
<action name="hello" class="cn.xiangxu.action.HelloAction">
<result name="success">
/WEB-INF/msg.jsp
</result>
</action>
</package> </struts>

struts.xml (struts框架配置文件)

 package cn.xiangxu.action;

 public class HelloAction {
public String execute() {
System.out.println("hello struts2"); return "success";
}
}

控制类

 <%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=utf-8">
<title>Insert title here</title>
</head>
<body>
<h2>Hello Struts2</h2>
</body>
</html>

msg.jsp (结果页面)

最新文章

  1. Hello cnblog!
  2. java io流 运行错误时,保存异常到文件里面
  3. scp与rsync限速
  4. 每日一九度之 题目1031:xxx定律
  5. Eclipse中添加web dynamic project
  6. addClass 函数
  7. OS.ENVIRON()详解
  8. MySQL 执行计划explain详解
  9. cf581D Three Logos
  10. csdn肿么了,这两天写的博文都是待审核
  11. MVP学习笔记——参考Google官方demo
  12. KXO151 Programming &amp; Problem Solving
  13. MySQL安装教程图解
  14. pm2启动jenkins不存在tty的问题
  15. [日常] Go语言圣经-函数递归习题
  16. Spark 编程模型(中)
  17. linux 使用NSF 映射远程磁盘目录
  18. SQL优化:使用explain
  19. django drf JWT
  20. Spring学习(七)-----Spring Bean的5种作用域

热门文章

  1. ural 2012 About Grisha N.(水)
  2. 编译内核时覆盖KBUILD_BUILD_USER和KBUILD_BUILD_HOST
  3. 从virustotal上下载病毒样本
  4. this license has been cancelled
  5. SMB/CIFS协议简介
  6. hdu-1012-u Calculate e(水题)
  7. CERC2016 爵士之旅 Jazz Journey
  8. django的get_or_create
  9. C++对C语言的拓展(5)—— 函数重载和函数指针结合
  10. ASP.NET MVC 缓存Outputcache (局部动态)