码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2——Action(二)

时间:2014-12-16 08:46:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:struts2   action   dmi   

五、动态方法调用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/csshref="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/csshref="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/csshref="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/csshref="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/csshref="styles.css">

-->

 

  </head>

  

  <body>

   user add success <br>

  </body>

</html>

 

(5)浏览器端输入

 

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

 bubuko.com,布布扣

Struts2——Action(二)

标签:struts2   action   dmi   

原文地址:http://blog.csdn.net/yaguanzhou2014/article/details/41951701

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!