一、jsp向服务器提交参数的方法如下:
- Form表单提交。
- request.setAttribute();和request.getAttribute();
- URL传值。
- <jsp:param>
<jsp:forward page="getParam.jsp">
<jsp:param name="name" value="111"/>
<jsp:param name="password" value="123"/>
</jsp:forward>
二、action接收参数的方式有如下三种。
1. action类中设置接受参数的同名private属性,生成相关属性的set、get方法。调用set方法自动把jsp参数值传给属性。
jsp页面如下:
<html>
<head></head>
<body>
<form action="login" method="login">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="submit" type="submit" />
</form>
</body>
</html>
action类如下:
public class UserAction extends ActionSupport {
private String username;
private String password;
private void setUsername(String username) {
this.username = username;
}
private String getUsername() {
return username;
}
private void setPassword(String password) {
this.password = password;
}
private String getPassword() {
return password;
}
}
不需要生成实体类。
2. 域模型(DomainModel)
jsp页面如下(参数名称改变了):
<html>
<head></head>
<body>
<form action="login" method="login">
<input name="user.username" type="text" />
<input name="user.password" type="password" />
<input name="submit" type="submit" />
</form>
</body>
</html>
action类如下:
public class UserAction2 extends ActionSupport {
private User;
private void setUser(User user) {
this.username = username;
}
private User getUser() {
return user;
}
}
实体类如下:
public class User {
private String username;
private String password;
private void setUsername(String username) {
this.username = username;
}
private String getUsername() {
return username;
}
private void setPassword(String password) {
this.password = password;
}
private String getPassword() {
return password;
}
}
3. 模型驱动(ModelDriven)
体现MVC思想。action类实现ModelDriven接口。
jsp页面如下:
<html>
<head></head>
<body>
<form action="login" method="login">
<input name="username" type="text" />
<input name="password" type="password" />
<input name="submit" type="submit" />
</form>
</body>
</html>
action类如下(实体类实例化):
public class UserAction3 extends ActionSupport implement ModelDriven {
private User user = new User();
@overwrite
public User getModel() {
return user;
}
}
实体类如下:
public class User {
private String username;
private String password;
private void setUsername(String username) {
this.username = username;
}
private String getUsername() {
return username;
}
private void setPassword(String password) {
this.password = password;
}
private String getPassword() {
return password;
}
}
网友评论