• Struts2之动态方法调用


    1.感叹号

    前台页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="user!login.action">登录</a><br>
    <a href="user!register.action">注册</a>
    </body>
    </html>
    

    struts.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    
    	<action name="user" class="cn.jiangwenwen.action.UserAction">
    		<result name="test">/test.jsp</result>
    		<allowed-methods>login,register</allowed-methods>
    	</action>
    </package>
    </struts> 
    

    Action方法

    package cn.jiangwenwen.action;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UserAction extends ActionSupport{
    	
    	public String login() {
    		
    		System.out.println("这是登陆!");
    		
    		return "test";
    		
    	}
    	
    	public String register() {
    		
    		System.out.println("这是注册!");
    		
    		return "test";
    			
    	}
    	
    
    }
    
    

    2.通配符

    jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <a href="user_login.action">登录</a><br>
    <a href="user_register.action">注册</a>
    </body>
    </html>
    

    struts.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="jiangwenwen" namespace="/" extends="struts-default">
    	   <!--使用通配符优化上面的步骤操作,{1}代表*第1次出现的位置-->
    	 <action name="user_*" class="cn.jiangwenwen.action.UserAction" method="{1}">
                <result name="test">/test.jsp</result>
                <allowed-methods>login,register</allowed-methods>
            </action>
    </package>
    </struts> 
    
  • 相关阅读:
    数字以万做单位——Java
    创建二维码工具类——Java
    Java 截取指定长度的字符串
    堆排序
    context:component-scan 的使用说明
    @Autowired @Resource @Qualifier的区别
    声明对象和创建对象的区别
    maven 命令
    maven 创建
    maven pom.xml文件
  • 原文地址:https://www.cnblogs.com/jiangwenwen1/p/9460175.html
Copyright © 2020-2023  润新知