struts2框架怎么运用

发布时间:2022-09-29 15:08:15 作者:iii
来源:亿速云 阅读:133

这篇文章主要讲解了“struts2框架怎么运用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“struts2框架怎么运用”吧!

创建WEB工程

这步还是很正常一样,创建一个普通web工程。如图:

struts2框架怎么运用

导入必要jar包

首先我要告诉你到哪里去下载这个框架:在apache官网,你百度struts2下载就可以找到了。

struts2框架怎么运用

Struts运行必要jar包介绍:

struts2框架怎么运用

开发中为了方便导入,可以使用app/struts2-blank.war携带jar包

编写JSP页面

在webRoot下新建一个Demo文件,网页先访问start.jsp之后通过框架返回到result页面

struts2框架怎么运用

start.jsp界面:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
 <head>
 </head>
 <body>
  <h1>请求发起的页面</h1>
  <!-- 发起一个请求 访问Struts2框架 -->
  <!-- Struts2请求 默认是以.action结尾 -->
  <a href="${pageContext.request.contextPath }/hello.action" rel="external nofollow" >访问Struts2入门程序</a>
 </body></html>

result.jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><html>
 <head>
 </head> 
 <body>
  <h1>处理完成的结果页面</h1>  
 </body></html>

编写Action服务器端处理逻辑

这里就是通过java创建一个类,该类可以说是action最普通的一个类

package com.yudian.struts;public class HelloAction {   public String execute() throws Exception{ //这里取execute代表默认执行这个方法
      System.out.println("我是执行的内容...");       return "excutesuccess";  //有返回值,我们用来跳转页面
    }
}

进行框架配置web.xml、struts.xml

配置文件才是核心关键,先配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" 
  xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 <display-name></display-name>  
 <!-- 配置Struts2的前端控制器 --><!--filter-class里面很关键,固定 -->
  <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>
 <welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
 </welcome-file-list></web-app>

在配置struts.xml,注意了这个是放在src下面而不是包下面

<?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="default" extends="struts-default">
   <!-- 3.默认Action 和Action的默认处理类 -->
    <!-- 1.初步认识Struts2 --><!-- 这里hello一定要和jsp中hello。action一致才能找到 -->
    <action name="hello" class="com.yudian.struts.HelloAction">
      <result name="excutesuccess">/demo/result.jsp</result>
    </action> <!-- result代表如果返回值为 excutesuccess则跳转页面-->      
  </package></struts>

运行测试

先通过浏览器访问start界面:

struts2框架怎么运用

当点击:访问Struts2入门程序连接直接跳转到了result.jsp

运行结果:

struts2框架怎么运用

感谢各位的阅读,以上就是“struts2框架怎么运用”的内容了,经过本文的学习后,相信大家对struts2框架怎么运用这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

推荐阅读:
  1. 模块及运用
  2. arp协议及运用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

struts2

上一篇:win10电脑中怎么打开运行命令

下一篇:win10笔记本插电源就会蓝屏如何解决

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》