zoukankan      html  css  js  c++  java
  • Struts2初级篇(HelloWorld)

    Struts2的工作流程:

    从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的:

    • 操作(Actions)
    • 拦截器(Interceptors)
    • 值栈(Value Stack)/OGNL
    • 结果(Result)/结果类型
    • 视图技术(jsp/freemark)
    而Struts2 与传统的MVC框架略有不同,因为它由Action扮演模型的角色,而不是控制器,

      浏览器发送请求,首先被配置在web.xml中的Dispatcher Filter过滤【Controller层】,然后根据用户请求的URL(也可以说是action name),到struts2的配置文件struts.xml文件中查找,该action name对应的action,并执行该action指定的方法【这一步骤对应上图中的Action,准备视图要展示的数据,称之为Model层】,方法执行完毕后会返回一个字符串(逻辑视图名),该字符串和配置在action中的result的name属性对应,跳转到真实的视图页面,到达页面后通过"值栈"/OGNL表达式从Model层中获取数据【View层】。

    导入Struts2所需的jar包:

    • commons-fileupload-x.y.z.jar
    • commons-io-x.y.z.jar
    • commons-lang-x.y.jar
    • commons-logging-x.y.z.jar
    • commons-logging-api-x.y.jar
    • freemarker-x.y.z.jar
    • javassist-x.y.z.GA
    • ognl-x.y.z.jar
    • struts2-core-x.y.z.jar
    • xwork-core.x.y.z.jar

    一:创建Action

    HelloWorldAction.java

     1 package cn.woo.action;
     2 
     3 /**
     4  * @Description:TODO HelloWorldAction
     5  * @author: wyh
     6  * @version: V1.0  
     7  * @Date: 2018年8月25日 上午11:16:20 
     8  */
     9 public class HelloWorldAction {
    10     
    11     private String name;
    12 
    13     public String getName() {
    14         return name;
    15     }
    16 
    17     public void setName(String name) {
    18         this.name = name;
    19     }
    20     
    21     public String execute() throws Exception{
    22         return "success";
    23     }
    24 }

     二:首页:index.jsp【入口页】

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!-- 使用Struts2的标签库 -->
     4 <%@ taglib uri="/struts-tags" prefix="s"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     9 <title>数据展示页</title>
    10 </head>
    11 <body>
    12     欢迎:<s:property value="name"/>
    13 </body>
    14 </html>

    三:欢迎页面【登陆成功后访问的页面】HelloWorld.jsp

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!-- 使用Struts2的标签库 -->
     4 <%@ taglib uri="/struts-tags" prefix="s"%>
     5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     6 <html>
     7 <head>
     8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     9 <title>数据展示页</title>
    10 </head>
    11 <body>
    12     欢迎:<s:property value="name"/>
    13 </body>
    14 </html>

    四:编写Struts2的配置文件 struts.xml【默认要放置在WEB-INF/classes文件夹下,该位置不固定,只要在web.xml文件中配置的时候指定就行。】

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE struts PUBLIC
     3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4    "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 <struts>
     6 <!-- 设置struts.devMode为true表示,表明当前处于开发模式可以输出日志信息 -->
     7 <constant name="struts.devMode" value="true" />
     8     <!-- 创建包,目的是划分一个区域,将多个Action区分开来,就像项目中的包【action多的时候,这是进行模块化的一种方式;还有一种方式就是将该配置文件查分成不同作用的多个文件,然后在每个struts配置文件的尾部引入后续的配置文件即可】 -->
     9    <package name="helloworld" extends="struts-default">
    10            
    11          <!-- 定义索引操作,之前访问首页的时候直接使用的是index.jsp,现在访问的时候可以使用index.action的方式进行访问 -->
    12          <action name="index">
    13              <result>/index.jsp</result>
    14          </action>
    15    
    16          <!-- 访问的时候使用的URL就是hello,访问hello的时候,会被Struts的过滤器和拦截器进行拦截,找到action name对应的action类 
    17                  如: cn.woo.action.HelloWorldAction,然后会执行method对应的方法,执行完毕后返回的String[也就是逻辑视图名],会和下方
    18            (<result>)中的name对应,然后跳转到真正的视图:HelloWorld.jsp中-->
    19       <action name="hello" 
    20             class="cn.woo.action.HelloWorldAction" 
    21             method="execute">
    22             <result name="success">/HelloWorld.jsp</result>
    23       </action>
    24    </package>
    25 </struts>

    五:配置web.xml文件

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <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">
     3   <display-name>Struts2Demo</display-name>
     4   <welcome-file-list>
     5     <welcome-file>index.html</welcome-file>
     6     <welcome-file>index.htm</welcome-file>
     7     <welcome-file>index.jsp</welcome-file>
     8     <welcome-file>default.html</welcome-file>
     9     <welcome-file>default.htm</welcome-file>
    10     <welcome-file>default.jsp</welcome-file>
    11   </welcome-file-list>
    12   
    13   <!-- 配置Struts的入口:FilterDispatcher过滤器 -->
    14   <filter>
    15       <filter-name>struts2</filter-name>
    16       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    17   </filter>
    18 
    19   <filter-mapping>
    20       <filter-name>struts2</filter-name>
    21       <!-- 配置过滤所有满足/*的请求 -->
    22       <url-pattern>/*</url-pattern>
    23   </filter-mapping>  
    24 </web-app>

    六:配置日志输出路径logging.properties【默认放置在WEB-INF/classes文件夹中,如果文件夹不存在可自行配置】

    1 org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
    2 org.apache.catalina.core.ContainerBase.[Catalina].handlers = 
    3                               java.util.logging.ConsoleHandler
  • 相关阅读:
    CoreJava Reading Note(9:Collection)
    CoreJava Reading Note(8:Generic programming)
    Algorithms 4th Reading Note(3:Find)
    CoreJava Reading Note(7:Exception,Assertions,Logging)
    Algorithms 4th Reading Note(1:Foundation)
    CoreJava Reading Note(6:Interface,lambda and Inner Class)
    Algorithms 4th Reading Note(2:Sort)
    CoreJava Reading Note(5:Inheritance)
    SpringMVC spring-servlet.xml配置
    MySQL 数据库事物隔离级别的设置
  • 原文地址:https://www.cnblogs.com/wooyoohoo/p/9536411.html
Copyright © 2011-2022 走看看