zoukankan      html  css  js  c++  java
  • Struts2框架简介和示例

    struts2框架

    Struts2是java web的框架,在Java Web开发中,表示层框架,其核心是通过扩展Servlet来帮助处理http请求。

    Struct2的基本流程

    Struct2的框架由3个部分组成:核心控制器FilterDispatcher、业务控制器、和用户实现的业务逻辑组件,其基本流程为:FilterDispatcher->Action->业务逻辑组件。核心控制器负责拦截所有的用户请求,当请求是*.action结尾会被转入Struts2框架处理,Struts2再决定调用哪个业务逻辑组件。业务控制器就是实现Action类的实例,Action类通常包含一个execute方法(也可在配置文件中指定方法执行),该方法返回一个逻辑视图名的字符串。

    Action

    Struts2使用Action类封装HTTP请求参数,因此Action类里包含与请求参数对应的属性,并提供getter和setter方法。Action类通常包含一个execute方法,该方法返回一个为逻辑视图名的字符串(error、none、input、login、success),Action类也可以在Struts2.xml的action元素中配置method属性来指定方法调用。

    Java Web开发环境

    1、安装jdk
    2、安装tomcat
    3、配置环境

    请戳:Java Web环境配置

    Web工程

    新建一个web工程,工程名为WebappTest

    导入struts2的核心jar包

    编写Action类和配置文件

    编写action类
    package com.example.action;
    import java.io.Serializable;
    import com.opensymphony.xwork2.ActionSupport;
    public class HelloAction extends ActionSupport implements Serializable{
    	
    	/*
    	*封装用户请求参数
    	*提供getter和setter方法
    	*/
    	private String message;
    	public String getMessage() {
    		return message;
    	}
    	public void setMessage(String message) {
    		this.message = message;
    	}
    	//编写struts2.xml指定的方法,返回逻辑视图名称
    	public String sayHello(){
    		message = "Hello Struts2";
    		return SUCCESS;
    	}
    }
    
    
    编写action处理后跳转的jsp
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="ISO-8859-1"%>
    <!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=ISO-8859-1">
        <title>Insert title here</title>
        </head>
        <body>
            ${message}
        </body>
    </html>
    
    配置action

    Struts2使用package来管理Action和拦截器,每个包就是多个Action、多个拦截器及其引用的集合,定义pacckage元素是指定如下几个属性:

    • name:名字,可用于其他包的引用
    • extends:继承至其他包
    • namespace:定义命名空间

    使用result来配置结果,Struts2支持JSP、Velocity、FreeMaker多种视图技术,这里的结果使用JSP视图。

    编写struts.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="song" namespace="/test" extends="struts-default">
    			    <!--指定Action要调用的方法为sayHello-->
    				<action name="hellostruts2" class="com.example.action.HelloAction" method="sayHello">
    				    <!--为success的逻辑视图配置Result-->
    					<result name="success">/success.jsp</result>
    				</action>
    			</package>
    		</struts>
    	
    
    在web.xml配置文件下添加配置项
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" 
    	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/javaee 
    	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
      <display-name></display-name>
      <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>
    
    
    执行

    开启tomcat服务器,在浏览器中输入

    http://localhost:8080/WebappTest/test/hellostruts2
    

    跳转到jsp界面

  • 相关阅读:
    五险一金的详细解释
    Android源码下载和编译Tips
    C++ STL遍历map的时候如何删除其中的element
    Android 2.3.4 RTSP的实现不在StageFright中,在opencore中
    找不到显示桌面的快捷方式怎么办|显示桌面的快捷方式找不到解决方法|显示桌面代码|
    选择适合过一辈子的人
    .net兼职人员| .net兼职系统开发人员| .net兼职开发人员
    skype帐号|超值skype帐号|14分钟skype账号|1元40个|5毛20个|15天有效期
    黄金市场的时间段分析
    解决导航问题winform的左侧树控件右侧panel加载用户控件
  • 原文地址:https://www.cnblogs.com/sweiqi/p/5936638.html
Copyright © 2011-2022 走看看