1.使用類接收
Action代碼
package com.test.action;
import Model.Teacher;
public class TeacherAction {
private Teacher teacher;
public Teacher getTeacher(){
return teacher;
}
public void setTeacher(Teacher teacher){
this.teacher=teacher;
}
public String add(){
System.out.println("name"+teacher.getUsername());
System.out.println("password"+teacher.getPassword());
return "success";
}
public String error(){
return "error";
}
}
Teacher代碼
package Model;
public class Teacher {
private String username;
private String password;
public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
}
http://172.17.32.112:8080/Struts2/test2/Teacher_add?teacher.username=a&teacher.password=123
得到
第二個方法:用屬性接收參數(要寫get和set)
public class TeacherAction {
private String username;
<span style="white-space:pre"> </span>private String password;
<span style="white-space:pre"> </span><pre code_snippet_id="1554438" snippet_file_name="blog_20160112_2_6365526" name="code" class="java"><span style="white-space:pre"> </span>public String getUsername(){
return this.username;
}
public String getPassword(){
return this.password;
}
public void setUsername(String username){
this.username=username;
}
public void setPassword(String password){
this.password=password;
}
public String add(){System.out.println("name"+this.username);System.out.println("password"+this.password);return "success";}public String error(){return "error";}}
輸入url
http://172.17.32.112:8080/Struts2/test2/Teacher_add?username=a&password=123
得到結果一樣。
package com.test.action;
import com.opensymphony.xwork2.ModelDriven;
import Model.Teacher;
public class TeacherAction implements ModelDriven<Teacher> {
private Teacher teacher = new Teacher();
public String add(){
System.out.println("name"+teacher.getUsername());
System.out.println("password"+teacher.getPassword());
return "success";
}
public String error(){
return "error";
}
public Teacher getModel(){
return teacher;
}
}
url輸入:
http://172.17.32.112:8080/Struts2/test2/Teacher_add?username=a&password=123.
這樣做的好處:
在jsp頁面可以再次利用這些參數
總結方法1比較好.