zoukankan      html  css  js  c++  java
  • struts2框架的第一个程序

    helloworld.jsp在base目录下

    <a href="${pageContext.request.contextPath }/base/HelloWorldAction.action">有命名空间测试</a><br/>
    	<a href="${pageContext.request.contextPath }/HelloWroldAction.action">无命名空间测试</a><br/>
    

     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
    			1、为包,能方便进行action的管理
    			2、属性
    				name:
    					代表包的名称
    					主要用于继承
    					name的名称是唯一的
    				namespace:
    					命名空间
    					在客户端访问action时使用,也就是说在url中使用
    					如果没有命名空间
    						http://localhost:8080/struts2/a.action
    						http://localhost:8080/struts2/b.action
    					上面写的url没有模块的概念,但是如果有命名空间,就有模块的概念
    					如果namespace有值,则在result中,会把命名空间加入到响应的路径中
    				extends
    					配置文件中的继承
    	 -->
    	<package name="helloworld" namespace="/base" extends="struts-default">
    		<!-- 
    			action:
    				一个action配置代表一个类
    				属性
    					name  为action的名称
    					clas  为类的全名
    		 -->
    		<action name="HelloWorldAction" class="com.henau.action.base.HelloWorldAction">
    			<!-- 
    				result
    					struts2会根据result进行转发或者重定向
    					属性
    						name
    							为result名称
    							属性的值和action中execute方法的返回值一致
    							如果不写name属性,则会默认为name的值为success
    						type
    							代表返回方式
    								选择重定向还是转发,还可以重定向到action
    							如果type没有设定,则为默认值。这个默认值可以从struts-default.xml中得出结论
    							<result-type name="dispatcher" 
    								class="org.apache.struts2.dispatcher.ServletDispatcherResult" 
    								default="true"/>
    			 -->
    			<result name="success">success.jsp</result>
    		</action>
    	</package>
    </struts>
    

     web.xml的配置

    <filter>
          <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
          <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>StrutsPrepareAndExecuteFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
    

     HelloworldAction.java注意所在包

    package com.henau.action.base;
    
    import com.opensymphony.xwork2.Action;
    
    public class HelloWorldAction implements Action {
    
    	@Override
    	public String execute() throws Exception {
    		System.out.println("调用了HelloWorldAction");
    		return SUCCESS;
    	}
    
    }
    

     如果没有命名空间,namespace为/,在result中配置的是/base/success.jsp

  • 相关阅读:
    关于CI/CD/CD (Continuous Integration/Continuous Delivery/Continuous Deployment)
    linux bash变量替换(# ## % %% / //)
    Azure静态公网ip自助反解
    Hadoop HDFS HA启动出现两个StandBy NameNode
    yum反查某个命令或so库在哪个包里面
    Hadoop 新建集群namenode format
    命令行web客户端与HTTP REST API调试工具
    Linux下source文件两种方法
    Redis配置参数汇总
    Jenkins RestAPI调用出现Error 403 No valid crumb was included in the request
  • 原文地址:https://www.cnblogs.com/lzzhuany/p/4984614.html
Copyright © 2011-2022 走看看