Action 處理請求引數-屬性驅動模型驅動
阿新 • • 發佈:2019-01-04
1.屬性驅動
User.java
package com.qwl.domain; 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; } }
UserLogin.java
package com.qwl.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.qwl.domain.User; public class UserLoginAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String excute() throws Exception{ ActionContext context =ActionContext.getContext(); if("itcast".equals(user.getUsername())&&"123".equals(user.getPassword())){ context.getSession().put("username", user.getUsername()); context.getSession().put("password", user.getPassword()); return SUCCESS; }else{ context.getSession().put("error", "使用者登入失敗"); return ERROR; } } }
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="struts" extends="struts-default"> <action name="userlogin" class="com.qwl.action.UserLoginAction"> <result name="success">/loginSuccess.jsp</result> <result name="error">/loginError.jsp</result> </action> </package> </struts>
userLogin.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>
<div align="center">
<form name="form1" action="userlogin.action" method="post">
使用者名稱:<input type="text" name="user.username"><br>
密碼:<input type="password" name="user.password"> <br>
<input type="submit" value="登入">
</form>
</div>
</body>
</html>
2.模型驅動
User.java
package com.qwl.domain;
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;
}
}
UserLoginAction.java
package com.qwl.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qwl.domain.User;
public class UserLoginAction extends ActionSupport implements ModelDriven<User>{
private User user=new User();
public User getModel(){
return user;
}
public String excute() throws Exception{
ActionContext context =ActionContext.getContext();
if("itcast".equals(user.getUsername()) && "123".equals(user.getPassword())){
context.getSession().put("username", user.getUsername());
context.getSession().put("password", user.getPassword());
return SUCCESS;
}else{
context.getSession().put("error", "使用者登入失敗");
return ERROR;
}
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="struts" extends="struts-default">
<action name="userlogin" class="com.qwl.action.UserLoginAction">
<result name="success">/loginSuccess.jsp</result>
<result name="error">/loginError.jsp</result>
</action>
</package>
</struts>
userLogin.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>
<div align="center">
<form name="form1" action="userlogin.action" method="post">
使用者名稱:<input type="text" name="username"><br>
密碼:<input type="password" name="password"> <br>
<input type="submit" value="登入">
</form>
</div>
</body>
</html>