Struts2——Action(二)

五、动态方法调用Dynamic Method Invocation

采用“action!方法名称” 方式调用

范例:

(1)编写UserAction

package com.zgy.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

public String add(){

return SUCCESS;

}

}

(2)配置struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="user" extends="struts-default" namespace="/user">

<action name="user" class="com.zgy.action.UserAction">

<result>/user_add_success.jsp</result>

</action>

</package>

</struts>

(3)编写user_add_success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 ‘user_add_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>

user add success <br>

</body>

</html>

(4)浏览器访问如下url

http://localhost:8080/DynamicMethodInvocation/user/user!add

六、通配符配置ActionWildcard

使用通配符可以将配置量降到最低。使用*匹配,用{n}的形式与第n个*匹配。

范例:

(1)编写StudentAction.java

package com.zgy.action;

public class StudentAction {

public String add(){

return "success";

}

public String delete(){

return "success";

}

}

(2)配置struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="actions" extends="struts-default" namespace="/actions">

<action name="Student*" class="com.zgy.action.StudentAction" method="{1}">

<result>/student_{1}_success.jsp</result>

</action>

<action name="*_*" class="com.zgy.{1}Action">

<result>/{1}_{2}_success.jsp</result>

</action>

</package>

</struts>

(3)编写student_add_sucess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 ‘studentadd_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>

student add success <br>

</body>

</html>

(4)编写student_delete_sccess.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 ‘student_delete_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>

student delete success<br>

</body>

</html>

(5)浏览器端分别访问

http://localhost:8080/ActionWildcard/actions/Studentdelete

http://localhost:8080/ActionWildcard/actions/Studentadd

七、用Action的属性接收参数

此种方法是在action类中添加属性,用于接受传递进来的参数

范例:

(1)编写UserAction.jsp

package com.zgy.action;

public class UserAction {

private String name ;

private int age;

public String add(){

System.out.println("name="+name);

System.out.println("age="+age);

return "success";

}

public String getName(){

return this.name;

}

public void setName(String name){

this.name = name;

}

public int getAge(){

return this.age;

}

public void setAge(int age){

this.age = age;

}

}

(2)配置struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="user" extends="struts-default" namespace="/user">

<action name="user" class="com.zgy.action.UserAction" method="add">

<result>/user_add_success.jsp</result>

</action>

</package>

</struts>

(3)编写user_add_success.jsp页面

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 ‘user_add_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>

user add success <br>

</body>

</html>

(4)浏览器端输入如下url

http://localhost:8080/ActionAttributeInput/user/user?name=zhangsan&age=24

八、使用域模型Domain Model接收参数

使用域模型接受参数,然后在action中调用域模型的对象

范例:

(1)创建User.java,包含两个属性:name/age

package com.zgy.domain;

public class User {

private String name ;

private int age ;

public String getName(){

return this.name;

}

public void setName(String name){

this.name = name;

}

public int getAge(){

return this.age;

}

public void setAge(int age){

this.age = age;

}

}

(2)创建UserAction.java,调用User

package com.zgy.action;

import com.zgy.domain.User;

public class UserAction {

private User user;

public String add(){

System.out.println("name="+user.getName());

System.out.println("age="+user.getAge());

return "success";

}

public User getUser(){

return user;

}

public void setUser(User user){

this.user = user;

}

}

(3)配置struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="user" extends="struts-default" namespace="/user">

<action name="user" class="com.zgy.action.UserAction" method="add">

<result>/user_add_success.jsp</result>

</action>

</package>

</struts>

(4)编写user_add_success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>

<%

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 ‘user_add_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>

user add success <br>

</body>

</html>

(5)浏览器端输入

http://localhost:8080/DomainModel/user/user?user.name=zhangsan&user.age=24

时间: 2024-08-10 02:10:15

Struts2——Action(二)的相关文章

Struts2(四)Action二配置

一.method参数 action package com.pb.web.action; public class HourseAction { public String add(){ System.out.println("执行添加操作!"); return "success"; } public String update(){ System.out.println("执行更新操作!"); return "success"

struts2 action在前台显示信息提示

一.前端解决 1.使用javascript代码验证,为空不允许提交.就算验证成功也不是表单提交成功了,只是肯定前端传来的数据是正确的. 2.使用ajax方式,action处理完成返回一个成功消息就可以了.失败就返回失败消息.(每个提交时都要用ajax,太麻烦) 二.后台解决 3.使用struts2自带功能.判断完参数后,如果为空,return到一个错误页面error,正常的话return到一个提交成功页面success. 第一种:在struts2 action中弹出Js脚本提示信息 // 让ac

关于Struts2 Action中get和set惹得祸。

代码: 1 public class RandomAction extends PageAction { 2 3 /**随机抽取**/ 4 private IRandomService randomService; 5 6 /**责任民警**/ 7 private IScZrmjService scZrmjService; 8 9 /**企业基本信息**/ 10 private IQyjbxxService qyjbxxService; 11 12 private User user; 13 1

struts2 action 页面跳转

struts2 action 页面跳转 标签: actionstruts2redirect 2013-11-06 16:22 20148人阅读 评论(0) 收藏 举报 (1)type="dispatcher" 为默认,用于jsp页面跳转<result name="success">/index.jsp</result> 完整的写法为: <result name="success" type="dispatc

struts2 action重定向action

一共有三种方式redirect,redirect-action,chain 区别如下 1 redirect:action处理完后重定向到一个视图资源(如:jsp页面),请求参数全部丢失,action处理结果也全部丢失. 2 redirect-action:action处理完后重定向到一个action,请求参数全部丢失,action处理结果也全部丢失. 3 chain:action处理完后转发到一个action,请求参数全部丢失,action处理结果不会丢失. 今天碰到的bug显示提示另一个act

struts2 action通配符

首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespace="/actions"> <action name="Student*"class="com.bjsxt.struts2.action.StudentAction" method="{1}"> <resu

Struts2(二)action的三种方式

一.普通java类 package com.pb.web.action; /* * 创建普通的java类 */ public class HelloAction1 { public String execute(){ return "success"; } } 二.实现Action接口 package com.pb.web.action; import com.opensymphony.xwork2.Action; /* * 第二种实现Action接口 */ public class

Java Struts2 (二)

二.封装请求正文到对象中(非常重要) 1.静态参数封装 在struts.xml配置文件中,给动作类注入值.调用的是setter方法. 原因:是由一个staticParams的拦截器完成注入的. 2.动态参数封装:开发时用到的 通过用户的表单封装请求正文参数. 2.1.动作类作为实体模型 实体模型:Entity,对应数据库中表的记录(注意类对应的是表结构,而对象对应的是一条记录) 原因:是由params拦截器完成的. 2.2.动作类和实体模型分开 问题: 由于我们没有初始化user对象,默认为nu

Struts2—Action

二.命名空间namespace ·命名空间namespace须要放在相应的package下 ·Namespace必须以"/开头". ·Result的name是"success"的<result>能够不写其name.即: <result name="success">与<result>效果同样. ·Namespace为空的情形是: 当找到url找到相应的namespace下的action时,假设资源没有找到.那么