【发布时间】:2016-02-17 09:27:07
【问题描述】:
我正在学习 struts2 中的表单验证。我已经成功实现它,当文本字段为空白时,它在 jsp 页面上显示错误消息。但现在我删除了<s:textfield>,而不是使用<input type="text"> 标签。现在这不起作用,任何人都可以告诉我如何实现这一目标吗?
以下是我的代码
@Namespace("/User")
@ResultPath("/")
@Result(name = "input", location = "pages/registration.jsp")
public class UserAction extends ActionSupport implements ServletRequestAware, ModelDriven<User>
{
/**
*
*/
private static final long serialVersionUID = 1L;
User user = new User();
private HttpServletRequest servletRequest;
List<User> users = new ArrayList<User>();
public User getModel()
{
return user;
}
public User getUser()
{
return user;
}
public void setUser(User user)
{
this.user = user;
}
public HttpServletRequest getServletRequest()
{
return servletRequest;
}
public void setServletRequest(HttpServletRequest servletRequest)
{
this.servletRequest = servletRequest;
}
public List<User> getUsers()
{
return users;
}
public void setUsers(List<User> users)
{
this.users = users;
}
@Action(value = "register", results = {@Result(name = "input", location = "pages/registration.jsp"), @Result(name = "success", location = "pages/registration.jsp")})
// @InputConfig(methodName = "registeruser")
public String register()
{
return SUCCESS;
}
@Action(value = "registeruser", results = {@Result(location = "pages/welcome.jsp")})
public String registeruser()
{
UserDao userDao = new UserDaoImpl();
try
{
String uploadFilePath = servletRequest.getSession().getServletContext().getRealPath("/").concat("/User/images");
CommonUtils.uploadFile(user.getFile(), uploadFilePath, user.getFileFileName());
userDao.save(user, uploadFilePath);
}
catch (Exception e)
{
System.out.println(e);
}
return SUCCESS;
}
public void validate()
{
System.out.println("in");
if ("".equals(user.getFirstName()))
{
addFieldError("firstName", "First name should not be blank");
}
if ("".equals(user.getLastName()))
{
addFieldError("lastName", "Last name should not be blank");
}
}
}
Jsp页面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!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=ISO-8859-1">
<title>Registration</title>
</head>
<body>
<form action="registeruser" method="post" enctype="multipart/form-data">
<s:actionerror/>
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="firstName" id="firstName"></td>
<%-- <td><s:textfield name="firstName"></s:textfield></td> --%>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="lastName" id="lastName"></td>
</tr>
<tr>
<td>Contact Number</td>
<td><input type="text" name="contactNumber" id="contactNumber"></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" id="email"></td>
</tr>
<tr>
<td>Address</td>
<td><textarea rows="5" cols="50" name="address" id="address"></textarea></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender" id="male" value="male">Male<br />
<input type="radio" name="gender" id="female" value="female">Female</td>
</tr>
<tr>
<td>Degree</td>
<td><input type="checkbox" name="degree" id="graduation"
value="graduation">Graduation<br />
<input type="checkbox" name="degree" id="postgraduation"
value="postgraduation">Post Graduation</td>
</tr>
<tr>
<td>Technology</td>
<td><select name="technology" id="technology">
<optgroup label="Java">
<option value="spring">Spring</option>
<option value="hibernate">Hibernate</option>
<option value="struts2">Struts2</option>
</optgroup>
<optgroup label=".Net">
<option value="asp">ASP</option>
<option value="vb.net">VB.NET</option>
</optgroup>
</select></td>
</tr>
<tr>
<td>Upload file</td>
<td><input type="file" name="file" id="file"></td>
</tr>
<tr>
<td><input type="submit" value="Save"></td>
</tr>
</table>
</form>
</body>
</html>
【问题讨论】:
-
得到答案:D 你可以使用
-
谢谢@Aleksandr M.. :)
-
如果它有效,请记住接受答案@PradeepZaptech
-
@goodyzain 我没有,因为他的评论(在我的回答后一分钟发布)报告了我用过的拼写错误(
标签: java html jsp struts2 struts-tags