1.通过request对象获取
2.通过set属性驱动获取(常用)
3.通过(user)对象驱动获取
4.通过ModelDriden(user)模型获取 (常用)
通过一个登录的案例,实现Action接收request请求参数
一.第1种方法:通过request对象获取
loginAction.java
public class LoginAction extends ActionSupport{
public String login() {
//获取request对象
HttpServletRequest request = ServletActionContext.getRequest();
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
return NONE;
}
}
struts.xml
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="p2" extends="struts-default">
<action name="login" class="com.lyf.web.action2.LoginAction" method="login"></action>
</package>
</struts>
login.jsp
<body>
<form action="${pageContext.request.contextPath}/login.action">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" name="登录">
</form>
</body>
二.第2种:通过属性(set)驱动
loginAction.java
public class LoginAction extends ActionSupport{
private String username;
private String password;
//通过属性set方法, 注入参数
public void setUsername(String username) {
System.out.println("setUsername");
this.username = username;
}
public void setPassword(String password) {
System.out.println("setpassword");
this.password = password;
}
public String login() {
System.out.println(username);
System.out.println(password);
return NONE;
}
}
struts2.xml
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="p2" extends="struts-default">
<action name="login" class="com.lyf.web.action2.LoginAction" method="login"></action>
</package>
</struts>
login.jsp
<body>
<form action="${pageContext.request.contextPath}/login.action">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" name="登录">
</form>
</body>
三.第3种方法:通过(user)对象驱动
User.java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
loginAction2.jsp
public class LoginAction2 extends ActionSupport{
//第三种方式:通过set模型
private User user;
public User getUser() {
System.out.println("getUser");
return user;
}
public void setUser(User user) {
System.out.println("setUser");
this.user = user;
}
public String login2() {
System.out.println(user);
return NONE;
}
}
struts.xml
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="p2" extends="struts-default">
<action name="login2" class="com.lyf.web.action2.LoginAction2" method="login2"></action>
</package>
</struts>
login2.jsp
使用域对象,所以属性名需要加对象名(user.name),再赋值。
<body>
<form action="${pageContext.request.contextPath}/login.action">
<!-- 这里值需要user引入-->
用户名<input type="text" name="user.username"><br>
密码<input type="password" name="user.password"><br>
<input type="submit" name="登录">
</form>
</body>
四:第4种:添加模型驱动,实现模型驱动接口(比较常用)
model包下 的User.java
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
}
loginAction.java
//实现model包下的User类
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
//一定返回user
return user;
}
public String login() {
System.out.println("login方法...");
System.out.println(user);
return NONE;
}
}
struts.xml
<struts>
<constant name="struts.devMode" value="true"></constant>
<package name="p1" extends="struts-default">
<action name="login" class="com.lyf.web.action2.LoginAction2" method="login"></action>
</package>
</struts>
login.jsp
<body>
<form action="${pageContext.request.contextPath}/login.action">
用户名<input type="text" name="username"><br>
密码<input type="password" name="password"><br>
<input type="submit" name="登录">
</form>
</body>