English 中文(简体)
Spring MVC 差错
原标题:Spring MVC error

我对计算机编程世界来说相当新,因此,这是一场争取真正深入了解许多概念的斗争。 我现在就我们正在执行的春天世界运动项目开展工作。 该项目的第一步是建立一个网站的标识网页。 我在进行分类后尝试了模拟地雷,但我似乎无法在我的网络浏览器中发现以下错误:

Unsupported auto value type java.lang.String for field injuryReports.Login.userName

这里是我的Login实体类别:

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;
    }
}

MyloginDao级:

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;
    }
}

我的后勤班子:

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;
    }
}

令我感到担忧的是,我希望提供尽可能多的信息。 我确实赞赏任何帮助!

最佳回答

不能使用@GeneratedValue。 它使用数据库序列或<代码>AUTOINCREMENT的特征,视数据库的基本引擎而定。

删除这一说明:

@Id
private String userName;

或用于ger/长期:

@Id @GeneratedValue
private int userId;
问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签