Struts2之动态方法调用(优点:调用同一个action中的多个方法不需要在配置文件中写多个指向相同action类的的action节点只需要一个action节点就行)

在表单action值里指定所调用的action中的哪个方法而不是借助配置文件action节点的method属性

1 UserAction类

package org.action;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import org.model.User;

import com.opensymphony.xwork2.ActionSupport;

import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends ActionSupport implements

ModelDriven<User/*这里需要填写Person对象*/> {

//这种实现ModelDriven的模型驱动需要的模型要实例化

private User user=new User();

public User getUser() {

return user;

}

public void setUser(User user) {

this.user = user;

}

public String add() {

return "add";

}

@Override

public String execute() throws Exception {

// TODO Auto-generated method stub

return "minus";

}

@Override

public User getModel() {

// TODO Auto-generated method stub

return user;

}

}

2model类

package org.model;

public class User {

private String name;

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

3struts.xml

<?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.enable.DynamicMethodInvocation" value="true" > </constant>

<package name="default" extends="struts-default">

<action name="getuser" class="org.action.UserAction">

<result name="add">/success.jsp</result>

<result name="minus">/error.jsp</result>

<interceptor-ref name="modelDriven"></interceptor-ref>

<interceptor-ref name="defaultStack"></interceptor-ref>

</action>

</package>

</struts>

4首页

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP ‘index.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

<form action="getuser!add" method="post">
注:action值可以改成getuser就会调用默认方法excute

用户名:<input type="text" name="user.name"><br>

密码:<input type="text" name="user.password">

<input type="submit" value="提交">

</form>

</body>

</html>

5 转发页面error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP ‘error.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

username:<s:property value="user.name" /><br>

password:<s:property value="user.password" />

</body>

</html>

6转发页面 success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="/struts-tags" prefix="s" %>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<base href="<%=basePath%>">

<title>My JSP ‘success.jsp‘ starting page</title>

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0">

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

</head>

<body>

用户名:<s:property value="user.name" /><br>

密码:<s:property value="user.password" />

</body>

</html>

7运行截图;

(1)默认调用excute方法转发到error.jsp

2调用add方法

时间: 2024-10-20 08:41:33

Struts2之动态方法调用(优点:调用同一个action中的多个方法不需要在配置文件中写多个指向相同action类的的action节点只需要一个action节点就行)的相关文章

[LeetCode] 116. 填充每个节点的下一个右侧节点指针

题目链接 : https://leetcode-cn.com/problems/populating-next-right-pointers-in-each-node/ 题目描述: 给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点.二叉树定义如下: struct Node { int val; Node *left; Node *right; Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点.如果找不到下一个右侧节点,则将 next

LeetCode OJ:Populating Next Right Pointers in Each Node II(指出每一个节点的下一个右侧节点II)

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could be any binary tree? Would your previous solution still work? Note: You may only use constant extra space. For example,Given the following binary tr

[leetcode-117]填充每个节点的下一个右侧节点指针 II

(1 AC) 填充每个节点的下一个右侧节点指针 I是完美二叉树.这个是任意二叉树 给定一个二叉树 struct Node { int val; Node *left; Node *right; Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点.如果找不到下一个右侧节点,则将 next 指针设置为 NULL. 初始状态下,所有 next 指针都被设置为 NULL. 示例:For example, Given the following binary tree

NodeVisitor的使用-遍历Geode节点并在它与父节点之间添加一个LOD节点

#include <osg\NodeVisitor>#include <osg\MatrixTransform>#include <osg\PagedLOD>#include <osgDB\FileNameUtils>#include <osg\Geode>#include <strstream> //只能处理osgExp插件导出的单个ive文件class InsideLODVisitor : public osg::NodeVisi

LeetCode 第117题 填充每个节点的下一个右侧节点指针||

给定一个二叉树 struct Node { int val; Node *left; Node *right; Node *next; } 填充它的每个 next 指针,让这个指针指向其下一个右侧节点.如果找不到下一个右侧节点,则将 next 指针设置为 NULL. 初始状态下,所有 next 指针都被设置为 NULL. 示例: 1 class Solution117 { 2 3 public Node connect(Node root) { 4 if (root == null) { 5 r

leetcode 116填充每个节点的下一个右侧节点指针

time O(n) ,sapce O(n) /* // Definition for a Node. class Node { public: int val; Node* left; Node* right; Node* next; Node() {} Node(int _val, Node* _left, Node* _right, Node* _next) { val = _val; left = _left; right = _right; next = _next; } }; */ /

116.填充每个节点的下一个右侧节点指针(DFS)

2019-12-27 08:52:23 解法1:DFS 递归解决 /* // Definition for a Node. class Node { public: int val; Node* left; Node* right; Node* next; Node() : val(0), left(NULL), right(NULL), next(NULL) {} Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {

extjs中treepanel属性和方法

1.Ext.tree.TreePanel 主要配置项: root:树的根节点.rootVisible:是否显示根节点,默认为true.useArrows:是否在树中使用Vista样式箭头,默认为false.lines:是否显示树线,默认为true.loader:树节点的加载器,默认为Ext.tree.TreeLoader. selModel:树的选择模式,默认为Ext.tree.DefaultSelectionModel.pathSeparator:树节点路径的分隔符,默认为“/”.single

Mybatis_review之配置文件中的objectFactory节点内容说明

Mybatis中有一个默认的创建类的对象,名字叫做DefaultObjectFactory,这个类用于负责创建对象实体类.从这个类的外部看,这个类的主要作用就是根据一个类的类型得到该类的一个实体对象,比如,我们给他一个Tiger的type,他将会给我们一个Tiger的实体对象,我们给他一个java.lang.List对象,他将会给我们一个List的实体对象.这个其实从它的名字上就能看出来. 这个类在官方文档中介绍到DefaultObjectFactory这个类的对象做的事情是非常少的,仅仅是创建