域模型:
package model;
public class User {
private String userName;
private String userPassword;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
package action;
import com.opensymphony.xwork2.ActionSupport;
import model.User;
public class LoginAction extends ActionSupport{
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String execute(){
System.out.println(user.getUserName());
System.out.println(user.getUserPassword());
return this.SUCCESS;
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title><s:text name="userlogin.title" /></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<s:form action="login.action" method="post">
<table>
<tr>
<td>
</td>
<td>
<s:textfield name="user.userName" label="用戶名"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<s:password name="user.userPassword" label="密碼"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="%{getText('userlogin.submit')}" />
</td>
</tr>
</table>
</s:form>
</center>
</body>
</html>
DTO:
ERROR com.opensymphony.xwork2.interceptor.ParametersInterceptor - Developer Notification (set struts.devMode to false to disable this message):
Unexpected Exception caught setting 'loginname' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'loginname' with value ['admin', ]
package model;
public class User {
private int userId;
private String userName;
private String userPassword;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
UserDTO類代碼:
package dto;
public class UserDto {
private String userName;
private String userPassword;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
}
LoginAction代碼:
package action;
import com.opensymphony.xwork2.ActionSupport;
import dto.UserDto;
import model.User;
public class LoginAction extends ActionSupport{
private User user= new User();
private UserDto udto;
public UserDto getUdto() {
return udto;
}
public void setUdto(UserDto udto) {
this.udto = udto;
}
public String execute(){
user.setUserName(udto.getUserName());
user.setUserPassword(udto.getUserPassword());
System.out.println(user.getUserName());
System.out.println(user.getUserPassword());
return this.SUCCESS;
}
}
Login.jsp代碼:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title><s:text name="userlogin.title" /></title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<s:form action="login.action" method="post">
<table>
<tr>
<td>
</td>
<td>
<s:textfield name="udto.userName" label="用戶名"/>
</td>
</tr>
<tr>
<td>
</td>
<td>
<s:password name="udto.userPassword" label="密碼"/>
</td>
</tr>
<tr>
<td colspan="2" align="right">
<s:submit value="%{getText('userlogin.submit')}" />
</td>
</tr>
</table>
</s:form>
</center>
</body>
</html>