2016-06-04 3 views
0

내가 가진 null 인 엔티티 :ID 및 기타 분야는

@RequestMapping(value = "/edit/{newsId}", method = GET) 
public String edit(@PathVariable long newsId, Model model) 
{ 
    News news = newsService.getById(newsId); //hibernate 
    model.addAttribute("news", news); 
    return "admin"; 
} 

형태 :

 <form method = "POST" th:object = "${news}" th:action="@{/news/editNews}" id = "add_news_form"> 

       <input type = "text" th:field = "*{title}" th:value="*{title}" /> 

       <input type = "submit" value = "Edit" /><br> 

       <textarea name = "news_content" rows = "20" cols = "80" th:field = "*{content}" th:text= "*{content}" form = "add_news_form" >Wpisz... </textarea> 
     </form> 

@Entity 
public class News 
{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    private Long id; 
    private String title; 
    private String content; 
    private boolean isMain; 
} 

편집 버튼이 나는 형태로 제목과 내용을 편집 할 수 있습니다 누른 후

및 POST 방법 :

@RequestMapping(value = "/editNews", method = POST) 
public String editNews(News news) 
{ 
    newsService.update(news); //hibernate 
    return "redirect:/"; 
} 

게시 메서드의 뉴스 개체에 제목 및 콘텐츠 필드가 null이 아니므로 null 예외가 발생합니다. ID 설정을위한 텍스트 입력을 추가 할 수는 있지만 사용자는이를 수행 할 수 없습니다. 그리고 100 개의 다른 필드가 있고 그 필드에서 저장하고 두 개만 변경하려면 어떻게해야합니까?

+0

컨트롤러의 폼에 매개 변수를 매핑하려고합니다. public string editNews (@ModelAttribute ("news") 뉴스 뉴스)' –

+0

가 작동하지 않습니다. – Bambelal

답변

0

아마도이 경우 RedirectModel을 사용해야합니까?