struts2接收参数的3中基本方式
struts2接收参数的3中基本方式
南桥北木 发表于11个月前
struts2接收参数的3中基本方式
  • 发表于 11个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

标题:腾讯云 新注册用户域名抢购1元起>>>   

1 action属性接收参数

Login.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> <form action="loginaction" method="post"> 姓名:<input type="text" name="username"/> 密码:<input type="password" name="password"/> <input type="submit" value="提交"/> </form> </body> </html>

Struts.xml

<action name="loginaction" class="hello.com.LoginAction" method="Login"> <result>/success.jsp</result> </action>

LoginAction.java

public class LoginAction extends ActionSupport {

public String Login(){

   System.out.println(user.getUsername());
	return SUCCESS;
}

private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }

}

2 DoMainMethod方式接受参数

Login.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> <form action="loginaction" method="post"> 姓名:<input type="text" name="user.username"/>

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

<input type="submit" value="提交"/> </form> </body> </html>

Struts.xml <action name="loginaction" class="hello.com.LoginAction" method="Login"> <result>/success.jsp</result> </action> User.java package hello.com;

public class User { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } private String password; public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }

}

LoginAction.java package hello.com;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport { private User user;

public User getUser() {
	return user;
}


public void setUser(User user) {
	this.user = user;
}


public String Login(){

   System.out.println(user.getUsername());
	return SUCCESS;
}

}

3 ModeDriven实现参数传递(推荐使用)

Login.jsp

<body> <form action="loginaction" method="post">

 姓名:<input type="text" name="username"/>

密码:<input type="password" name="password"/>

<input type="submit" value="提交"/> </form> </body>

Struts.xml <action name="loginaction" class="hello.com.LoginAction" method="Login"> <result>/success.jsp</result> </action>

User.java package hello.com;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport { private User user;

public User getUser() {
	return user;
}


public void setUser(User user) {
	this.user = user;
}


public String Login(){

   System.out.println(user.getUsername());
	return SUCCESS;
}

}

LoginAction.java

package hello.com;

import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<User> {

private User user=new User();

public String Login(){

   System.out.println(user.getUsername());
	return SUCCESS;
}


public User getModel() {
	return user;
}

}

共有 人打赏支持
粉丝 0
博文 113
码字总数 19641
×
南桥北木
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: