복붙노트

[SPRING] Java - SpringMVC - 컨트롤러에서 매개 변수 가져 오기

SPRING

Java - SpringMVC - 컨트롤러에서 매개 변수 가져 오기

내 데이터베이스에 개체를 유지하는 데 문제가 있습니다. Profil과 다 대일 관계가있는 사용자가 있습니다.

내 JSP 그냥 새 사용자를 추가하십시오. 따라서 JSP에는 필드와 comboxBox가 ​​필요합니다. 문제는, 프로그램을 실행할 때 comboxBox에서 프로파일을 선택한 경우에도 user.profil 속성이 컨트롤러에서 NULL이라는 점입니다.

다음은 User 객체입니다.

package com.app.model;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class User {

    private long id;
    private String firstname;
    private String lastname;
    private String login;
    private String password;
    private Profil profil;

    public User() {
    }

    /**
     * @param firstname
     * @param lastname
     * @param login
     * @param password
     * @param profil
     */
    public User(String firstname, String lastname, String login,
            String password, Profil profil) {
        this.firstname = firstname;
        this.lastname = lastname;
        this.login = login;
        this.password = password;
        this.profil = profil;
    }

    /**
     * Get User Id
     * 
     * @return long - User Id
     */
    @Id
    @Column(name="id", unique = true, nullable = false)
    public long getId() {
        return id;
    }

    /**
     * Set User Id
     * 
     * @param long - User Id
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * Get User Firstname
     * 
     * @return String - User Firstname
     */
    @Column(name="firstname", unique = false, nullable = false)
    public String getFirstname() {
        return firstname;
    }

    /**
     * Set User Firstname
     * 
     * @param String - User Firstname
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * Get User Lastname
     * 
     * @return String - User Lastname
     */
    @Column(name="lastname", unique = false, nullable = false)
    public String getLastname() {
        return lastname;
    }

    /**
     * Set User Lastname
     * 
     * @param String - User Lastname
     */
    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    /**
     * @return the login
     */
    @Column(name="login", unique = true, nullable = false)
    public String getLogin() {
        return login;
    }

    /**
     * @param login the login to set
     */
    public void setLogin(String login) {
        this.login = login;
    }

    /**
     * @return the password
     */
    @Column(name="password", unique = false, nullable = false)
    public String getPassword() {
        return password;
    }

    /**
     * @param password the password to set
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * @return the profil
     */
    @ManyToOne( cascade = CascadeType.REFRESH, fetch = FetchType.EAGER )
    @JoinColumn( name = "fk_profil_id", nullable = false )
    public Profil getProfil() {
        return profil;
    }

    /**
     * @param profil the profil to set
     */
    public void setProfil(Profil profil) {
        this.profil = profil;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "User [id=" + id + ", firstname=" + firstname + ", lastname="
                + lastname + ", login=" + login + ", password=" + password
                + "]";
    }
}

이것은 Profil 객체입니다.

package com.app.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="profil")
public class Profil {

    private long id;
    private String name;

    public Profil() {
    }

    /**
     * @param id
     * @param name
     */
    public Profil(long id, String name) {
        this.id = id;
        this.name = name;
    }   

    /**
     * @return the id
     */
    @Id
    @Column(name="id", unique = true, nullable = false)
    public long getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(long id) {
        this.id = id;
    }

    /**
     * @return the name
     */
    @Column(name="name", unique = true, nullable = false)
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Profil [id=" + id + "]";
    }
}

컨트롤러입니다.

package com.app.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.app.model.User;
import com.app.service.impl.ProfilService;
import com.app.service.impl.UserService;

@Controller
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProfilService profilService;

    @ModelAttribute("userForm")
    public User createForm()
    {
        return new User();
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ModelAndView saveUser ( @ModelAttribute("userForm") User user, BindingResult result ) {

        userService.addUser( user );

        return new ModelAndView ( "redirect:/users.html" );
    }

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView viewUsers() {
        Map model = new HashMap();
        model.put ( "users", userService.getUsers() );

        return new ModelAndView ( "usersView", model );
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView addUser() {
        Map model = new HashMap();
        model.put ( "profils", profilService.getProfils() );

        return new ModelAndView ( "userAdd", model );
    }
}

다음은 JSP 페이지입니다.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add User</title>

</head>

<body>
<h1>Add User</h1>
<c:url var="viewUsersUrl" value="/users.html" />
<a href="${viewUsersUrl}">View Existing Users</a>

<br /><br />
<c:url var="saveUserUrl" value="/users/save.html" />
<form:form modelAttribute="userForm" method="POST" action="${saveUserUrl}">
    <form:label path="firstname">Firstname:</form:label>
    <form:input path="firstname"/><br />
    <form:label path="lastname">Lastname:</form:label>
    <form:input path="lastname"/><br />
    <form:label path="login">Login:</form:label>
    <form:input path="login"/><br />
    <form:label path="password">Password:</form:label>
    <form:input path="password"/><br />
    <form:select path="profil">  
        <form:option value="0" label="---- Select ----" />
        <form:options items="${profils}" itemValue="id" itemLabel="name" />   
    </form:select> 
    <input type="submit" value="Save User" />
</form:form>

</body>
</html>

예외 :

SEVERE: Servlet.service() for servlet [spring] in context with path [/app] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Column 'fk_profil_id' cannot be null; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Column 'fk_profil_id' cannot be null] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'fk_profil_id' cannot be null
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.Util.getInstance(Util.java:386)
...
...
...

이 예외는 user.profile이 컨트롤러에서 NULL이기 때문에 발생합니다.

더 많은 정보가 필요하면 언제든지 물어보십시오. 그리고 btw, 뭔가 잘못되거나 더 나은 방법을 발견하면 제게 말해주세요, 봄과 봄 - mvc에 새로 왔습니다.

고마워.

해결법

  1. ==============================

    1.JSP 페이지 콤보 상자에 아래 경로가 있어야합니다.

    JSP 페이지 콤보 상자에 아래 경로가 있어야합니다.

    <form:select path="profil.id">  
        <form:option value="0" label="---- Select ----" />
        <form:options items="${profils}" itemValue="id" itemLabel="name" />   
    </form:select>
    

    이렇게하면 스프링 전환이 profil의 새 인스턴스를 만들고 User와 연결하도록 요청합니다.

  2. from https://stackoverflow.com/questions/12425154/java-springmvc-get-parameter-in-the-controller by cc-by-sa and MIT license