七、接收参数
1.使用action的属性接收参数,只要action中属性名与jsp中控件名一致并实现类get,set方法即可直接使用
1.1webRoot下建login.jsp
<body>
<h2>用户登陆</h2>
<form action="LoginAction.action" method="post">
用户名:<input type="text" name="name"><br/>
密码:<input type="text"name="password"><br/>
<input type="submit" value="登录">
</form>
</body>
1.2新建Action
package com.myz.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String login(){
System.out.println("name="+name);
return SUCCESS;
}
}
1.3配置action
<action name="LoginAction" method="login" class="com.myz.action.LoginAction"> <result>/loginok.jsp</result> </action>
1.4WebRoot下新建loginok.jsp
1.5发布项目,浏览器访问http://localhost:8080/HelloWorld/login.jsp,输入用户名密码后跳转到登录成功页面,后台打出输入的用户名
2.使用DomainModel接收参数
2.1login.jsp
<body>
<h2>用户登陆</h2>
<form action="LoginAction.action" method="post">
用户名:<input type="text" name="user.name"><br/>
密码:<input type="text"name="user.password"><br/>
<input type="submit" value="登录">
</form>
</body>
2.2新建user类
package com.myz.domain;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.3新建、配置action,struts.xml同1.3
package com.myz.action;
import com.myz.domain.User;
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("name="+user.getName());
return SUCCESS;
}
}
2.4发布项目,浏览器访问http://localhost:8080/HelloWorld/login.jsp,输入用户名密码后跳转到登录成功页面,后台打出输入的用户名
3.使用ModelDriven接收参数(推荐)
3.1login.jsp
<body>
<h2>用户登陆</h2>
<form action="LoginAction.action" method="post">
用户名:<input type="text" name="name"><br/>
密码:<input type="text"name="password"><br/>
<input type="submit" value="登录">
</form>
</body>
3.2新建User类同2.2
3.3新建Action,配置action同1.3
package com.myz.action;
import com.myz.domain.User;
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("name="+user.getName());
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
3.4访问方式同上
二、集合对象接收参数
1.list接收参数
1.1login.jsp
<body> <h2>用户登陆</h2> <form action="LoginAction.action" method="post"> 用户名:<input type="text" name="name"><br/> 密码:<input type="text"name="password"><br/> 书籍1:<input type="text" name="booklist[0]"><br/> 书籍1:<input type="text" name="booklist[1]"><br/> <input type="submit" value="登录"> </form> </body>
1.2User.java
package com.myz.domain; import java.util.List; public class User { private String name; private String password; private List<String> booklist; public List<String> getBooklist() { return booklist; } public void setBooklist(List<String> booklist) { this.booklist = booklist; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
1.3loginAction.java
package com.myz.action; import com.myz.domain.User; 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("name="+user.getName()); System.out.println("book1="+user.getBooklist().get(0)); System.out.println("book1="+user.getBooklist().get(1)); return SUCCESS; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
1.4Action配置同上,浏览器访问同上
2.list对象中存放对象
2.1Book类
package com.myz.domain; public class Book { private String bookname; private int bookprice; public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public int getBookprice() { return bookprice; } public void setBookprice(int bookprice) { this.bookprice = bookprice; } }
2.2User类
package com.myz.domain; import java.util.List; public class User { private String name; private String password; private List<Book> booklist; public List<Book> getBooklist() { return booklist; } public void setBooklist(List<Book> booklist) { this.booklist = booklist; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
2.3login.jsp
<body> <h2>用户登陆</h2> <form action="LoginAction.action" method="post"> 用户名:<input type="text" name="name"><br/> 密码:<input type="text"name="password"><br/> 书籍1:<input type="text" name="booklist[0].bookname"><br/> 书籍1:<input type="text" name="booklist[1].bookname"><br/> <input type="submit" value="登录"> </form> </body>
2.4LoginAction.java
package com.myz.action; import com.myz.domain.User; 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("name="+user.getName()); System.out.println("book1="+user.getBooklist().get(0).getBookname()); System.out.println("book1="+user.getBooklist().get(1).getBookname()); return SUCCESS; } @Override public User getModel() { // TODO Auto-generated method stub return user; } }
2.5Action配置即浏览器访问方式同上