zoukankan      html  css  js  c++  java
  • struts2+convertion实现struts.xml的零配置

     这几天看了看struts2,有看了一些教程,发现零配置这个思想挺好的,可以简化好多代码!于是就学着做了起来。
    struts.xml配置文件

    Java代码 复制代码
    1. <?xml version="1.0" encoding="UTF-8" ?>   
    2. <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">   
    3. <struts>   
    4.     <constant name="struts.convention.result.path" value="/WEB-INF/jsp/"/><!-- 将跳转路径指向/WEB-INF/jsp文件夹下 -->   
    5. </struts>      
    <?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>
    	<constant name="struts.convention.result.path" value="/WEB-INF/jsp/"/><!-- 将跳转路径指向/WEB-INF/jsp文件夹下 -->
    </struts>    
    
    


    这里面基本空空,

    action文件

    Java代码 复制代码
    1. package com.song.action;   
    2.   
    3. import org.apache.struts2.convention.annotation.Action;   
    4. import org.apache.struts2.convention.annotation.Namespace;   
    5.   
    6. import com.opensymphony.xwork2.ActionSupport;   
    7.   
    8. public class Hello extends ActionSupport {   
    9.   
    10.     @Override  
    11.     public String execute() throws Exception {   
    12.         // TODO Auto-generated method stub   
    13.         System.out.println("hello this is execute method!");   
    14.         return SUCCESS;   
    15.     }   
    16.        
    17.     @Action("showthis")   
    18.     public String mymethod() throws Exception{   
    19.         System.out.println("-----------------------------------");   
    20.         return SUCCESS;   
    21.     }   
    22. }  
    package com.song.action;
    
    import org.apache.struts2.convention.annotation.Action;
    import org.apache.struts2.convention.annotation.Namespace;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class Hello extends ActionSupport {
    
    	@Override
    	public String execute() throws Exception {
    		// TODO Auto-generated method stub
    		System.out.println("hello this is execute method!");
    		return SUCCESS;
    	}
    	
    	@Action("showthis")
    	public String mymethod() throws Exception{
    		System.out.println("-----------------------------------");
    		return SUCCESS;
    	}
    }
    
    


    这里面定义了两个方法,然后我再web——info这个目录下,新建了一个jsp文件夹,再里面新建了一个hello.jsp和一个showthis.jsp
    之后再浏览器输入http://localhost:8088/test/hello.action,则调用execute方法,然后返回到hello.jsp页面;输入http://localhost:8088/test/showthis.action,则调用mymethod() ,页面跳转到showthis.jsp页面。
    注意:一定要导入struts2-convertion-plugs这个jar包,我用的struts2的jar包是2.1版本的

  • 相关阅读:
    前端编译原理 简述-jison
    GraphQL入门
    前端理解控制反转ioc
    window.onload和JQuery中$(function(){})的区别即其实现原理
    移动web之一像素问题
    display:table和display:table-cell的妙用
    sticky footer布局
    Elements in iteration expect to have 'v-bind:key' directives错误的解决办法
    vue模拟后台数据,请求本地数据的配置(旧版本dev-server.js,新版本webpack.dev.conf.js)
    使用vue-cli脚手架搭建项目,保存编译时出现的代码检查错误(ESLint)
  • 原文地址:https://www.cnblogs.com/studio313/p/1911927.html
Copyright © 2011-2022 走看看