欢迎光临散文网 会员登陆 & 注册

Java Web:STRUTS2基本配置,笔记,例子,讲义,视频,OGNL,pojo类【诗书画唱】

2021-01-02 15:28 作者:诗书画唱  | 我要投稿

本期看点:视频和视频笔记、 讲义、例子、Struts2框架概述PPT、OGNL、pojo类

友情提示:通过访问站点http://struts.apache.org/download.cgi#struts2320,可下载最新版本的Struts2框架的核心包。

视频和视频笔记START






视频和视频笔记END

讲义START



一、JAVA框架介绍

二、STRUTS2框架

JAVA:分为三大部分

JAVASE:JAVA基础,基本语法和OOP

JAVAEE:企业级开发,十三种技术规范,很多的JAVA框架

JAVAME:移动端开发,anroid开发需要java基础


JAVA框架中的三种典型框架:自学和举一反三的能力

struts1,struts2框架,springmvc框架:都是同一类的框架

springcore框架

hibernate框架,mybatis框架:都是同一类框架

框架都是项目开发的工具,框架没有那么难学

struts2框架:项目中的servlet会升级,看不到request和response了,getParameter消失了

springcore框架:理解框架和框架之间的粘合剂。

mybatis框架:数据库框架,这个框架可以让我们不再编写jdbc代码,dao类的代码全部消失了。

我们可以从以上的三种框架中各自选出一种框架,使用到我们的项目中。那么这个项目就是一个组合框架的项目。


微服务:springcloud


S4:js前端技能

java后台技能,背景调查,人工智能,大数据


Struts2框架中的所有的servlet会被升级为action(普通的java类)

在项目中使用Struts2框架的步骤:

1、导入struts2的框架包

2、打开web.xml文件,配置struts2框架的核心过滤器

3、将struts2框架的核心配置文件拷贝到src目录下


创建一个action,在后台能够打印出hello world,跳转到欢迎页面index.jsp

1、创建一个普通的java类,创建一个名为execute,返回值为String的无参的方法

2、在struts.xml文件中进行配置

3、启动服务器


创建一个action,在后台打印1+2等于多少,然后跳转到show.jsp页面



讲义END



例子START

在项目中使用Struts2框架的步骤:

可以“点”出(利用其提示)其内容。


访问action的方法:


跳转界面的方法START


跳转界面的方法END



package com.jy.action;


public class AddAction {

//http://localhost:8888/J190802/pub/addAc.action

    public String execute(){

    System.out.println(1 + 2);

    return "sh";

    }

}

package com.jy.action;

//action就是一个普通的java类

public class HelloAction {

//方法签名不能变

//返回值必须是String

//方法名必须是exectue

//没有参数

//http://localhost:8888/J190802/pub/hlAc.action

    public String execute(){

    System.out.println("Hello world");   

    return "wel";

    }

}



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

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">

<struts>

    <package name="my" namespace="/pub" extends="struts-default">

        <action name="hlAc" class="com.jy.action.HelloAction">

            <result name="wel">/index.jsp</result>

        </action>

        <action name="addAc" class="com.jy.action.AddAction">

            <result name="sh">/show.jsp</result>

        </action>

    </package>       

</struts>




<?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_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>J190802</display-name>

  <!-- 将Struts2框架引入项目中的配置 -->

  <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.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>

</web-app>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <base hreff="<%=basePath%>">

        <title></title>

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

        <meta http-equiv="description" content="This is my page">

    </head>

    <body>

        <h1>欢迎使用Struts2框架</h1>

    </body>

</html>

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

    String path = request.getContextPath();

    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

    <head>

        <base hreff="<%=basePath%>">

        <title></title>

        <meta http-equiv="pragma" content="no-cache">

        <meta http-equiv="cache-control" content="no-cache">

        <meta http-equiv="expires" content="0">

        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

        <meta http-equiv="description" content="This is my page">

    </head>

    <body>

        <h1>哇,好聪明</h1>

    </body>

</html>





例子END


Struts2框架概述PPT  START

dispatcher

调度员常用释义英 [dɪˈspætʃə(r)]美 [dɪsˈpætʃɚ]

  • n.

  • 调度员;[计] 调度程序;[计] 分配器


Struts2的action可以直接是一个pojo类

支持OGNL表达


描述:HttpServletRequest请求到达后首先经过ActionMapper,然后经过FilterDispatcher,如果还有其他的Filter,一次经过,最后到达一个叫ActionProxy的地方,这个时候通过ConfigurationManager读取配置文件struts.xml,然后到达ActionInvocation,开始经过一个个的Interceptor,最后达到Action进行处理后返回结果。

通过访问站点http://struts.apache.org/download.cgi#struts2320可下载最新版本的Struts2框架的核心包。


类似于servlet,访问struts的action路径为:http://[服务器端ip地址]:[服务器配置文件中指定的端口号]/[工程名][struts配置文件中的namespace属性]/[struts配置文件中需要访问的action的name属性].action。

Struts2框架概述PPT  END




Java Web:STRUTS2基本配置,笔记,例子,讲义,视频,OGNL,pojo类【诗书画唱】的评论 (共 条)

分享到微博请遵守国家法律