Java Spring MVC错误
我对计算机编程的世界几乎是一个全新的人,所以要真正深入理解许多概念是一件困难的事情。现在,我正在做一个项目,我们正在其中实现SpringMVC。项目的第一步是为网站创建登录页面。我试着模仿我们在课堂上所做的建模,但我似乎无法克服我的web浏览器中的以下错误:Java Spring MVC错误,java,hibernate,spring,spring-mvc,Java,Hibernate,Spring,Spring Mvc,我对计算机编程的世界几乎是一个全新的人,所以要真正深入理解许多概念是一件困难的事情。现在,我正在做一个项目,我们正在其中实现SpringMVC。项目的第一步是为网站创建登录页面。我试着模仿我们在课堂上所做的建模,但我似乎无法克服我的web浏览器中的以下错误: Unsupported auto value type java.lang.String for field injuryReports.Login.userName 这是我的登录实体类: package injuryReports;
Unsupported auto value type java.lang.String for field injuryReports.Login.userName
这是我的登录实体类:
package injuryReports;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Login implements Serializable {
private static final long serialVersionUID = 1L;
@Id @GeneratedValue
private String userName;
private String password;
private int userId;
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;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public Login() {
}
public Login(String userName, String password) {
super();
this.userName = userName;
this.password = password;
}
public Login(int userId, String userName2, String password2) {
this.userId = userId;
this.userName = userName2;
this.password = password2;
}
}
我的登录课程:
package injuryReports;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
*
* @author nGent
*
*/
@Component
public class LoginDao {
@PersistenceContext private EntityManager em;
@Transactional
public void persist(Login user) {
em.persist(user);
}
public List<Login> getAllUsers() {
TypedQuery<Login> query = em.createQuery(
"Select u FROM Login u ORDER BY u.id", Login.class);
return query.getResultList();
}
public Login validateLogin(String userName, String password) {
Login login = null;
TypedQuery<Login> query = em.createQuery(
"Select u From Login u where u.userName = :userName " +
" and u.password = :password", Login.class).setParameter(
"userName", userName).setParameter("password", password);
try {
login = query.getSingleResult();
}
catch (Exception e) {
//TODO: Handle Exception
}
return login;
}
}
和我的LoginController类:
package injuryReports;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class LoginController {
@Autowired
private LoginDao loginDao;
@RequestMapping(value = "/user", method = {RequestMethod.POST})
public ModelAndView userEntry(HttpServletRequest request) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if (userName != "" && password != "") {
loginDao.persist(new Login(userName, password));
}
return new ModelAndView("logon.jsp", "loginDao", loginDao);
}
@RequestMapping(value = "/login")
public ModelAndView login(HttpServletRequest request) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
String page = "login.jsp";
if (userName != "" && password != "") {
try {
Login login = loginDao.validateLogin(userName, password);
if (login != null) {
request.getSession().setAttribute("UserId", login.getUserId());
page = "login.jsp";
}
}
catch (Exception e) {
//TODO: Handle Exception
}
}
return new ModelAndView(page, getDaos());
}
@RequestMapping(value = "/logon", method = {RequestMethod.GET})
public ModelAndView logon(HttpServletRequest request) {
//int userId = (Integer) request.getSession().getAttribute("userId");
//request.getSession().setAttribute("UserID", userId);
return new ModelAndView("logon.jsp", getDaos());
}
public Map<String, Object> getDaos() {
Map<String, Object> models = new HashMap<String, Object>();
models.put("loginDao", loginDao);
return models;
}
}
抱歉,这有点长-我想提供尽可能多的信息。我真的很感激任何帮助 不能在字符串属性上使用。它使用数据库序列或自动增量功能,具体取决于基础数据库引擎
请删除此批注:
@Id
private String userName;
或使用整数/长作为id:
@Id @GeneratedValue
private int userId;
不能在字符串属性上使用。它使用数据库序列或自动增量功能,具体取决于基础数据库引擎
请删除此批注:
@Id
private String userName;
或使用整数/长作为id:
@Id @GeneratedValue
private int userId;
您注释了错误的ID字段;使用。。。好的,用户ID字段。您注释了错误的ID字段;使用。。。好的,userId字段。