您的位置:首页 > 编程语言 > Java开发

Struts2接收参数的三种方式

2015-12-03 19:11 411 查看
Struts2接收参数的三种方式:

1.使用action属性接收参数:

struts.xml

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

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

  <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

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

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

        

        <action name="user" class="hpu.edu.cn.action.UserAction">

            <result>

                /user_add_success.jsp

            </result>

        </action>

    </package>

    <!-- Add packages here -->

</struts>

index.jsp

<%@ 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>

    <a href="${pageContext.request.contextPath}/user/user!add?name=jack&age=8">添加学生</a><br>

  </body>

</html>

UserAction.java

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport{
private String name;
private String age;
public String add() {
System.out.println("name= "+name);
System.out.println("age= "+age);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

}

输出结果:

name= jack

age= 8

2.使用Domain Model接收参数

struts.xml

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

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

  <constant name="struts.enable.DynamicMethodInvocation" value="true" />

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

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

        

        <action name="user" class="hpu.edu.cn.action.UserAction">

            <result>

                /user_add_success.jsp

            </result>

        </action>

    </package>

    <!-- Add packages here -->

</struts>

UserAction.java

public class UserAction extends ActionSupport{
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;
}

}

User.java

public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

}

index.jsp

<%@ 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>

    <a href="${pageContext.request.contextPath}/user/user!add?user.name=jack&user.age=8">添加用户</a> <br>

  </body>

</html>

输出结果:

name= jack

age= 8

3.使用Model Driven接收参数(这种方式并不常用,但是隐含了一种非常重要的程序设计

思想)

struts.xml同上

UserAction.java

public class UserAction extends ActionSupport implements ModelDriven<User>{
User user = new User();
public String add() {
System.out.println("name= "+user.getName());
System.out.println("age= "+user.getAge());
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}

}

User.java

public class User {
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}

}

index.jsp

<%@ 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>

   <a href="${pageContext.request.contextPath}/user/user!add?name=jack&age=8">添加用户</a> <br>

  </body>

</html>

name= jack

age= 8
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  struts2.0