2017-09-15 24 views
0

我正在學習spring和thymeleaf並正在開展計時項目。 爲此,我需要驗證員工在一天內計時的小時數。Spring + thymeleaf Validanting整數錯誤BindingResult和bean名稱的普通目標對象都不作爲請求屬性

我用tutorial in the spring documentation這可是我不斷收到以下錯誤

Neither BindingResult nor plain target object for bean name 'timetable' available as request attribute 

任何想法我可能是做錯了?

Controller類

@RequestMapping(value="Timetable/AddToTimetable", method = RequestMethod.GET) 
public String newUser(Model md) { 
    md.addAttribute("assignments", serv.findAll()); 
    return "AddToTimetable"; 
} 

@RequestMapping(value = "/createEntry", method = RequestMethod.POST) 
public String create(@RequestParam("assignmentId") int assignmentId, 
        @RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date, 
        @RequestParam("hoursWorked") int hoursWorked, 
        @Valid Timetable timetable, BindingResult bindingResult, 
        Model md) { 
    timetable = new Timetable(); 
    timetable.setAssignmentId(assignmentId); 
    timetable.setDate(date); 
    timetable.setHoursWorked(hoursWorked); 
    md.addAttribute("timetables", service.timetableAdd(timetable)); 
if (bindingResult.hasErrors()) { 
     return "AddToTimetable"; 
    } 
    return "redirect:/Timetable"; 
} 

Service類

public BigInteger timetableAdd(Timetable timetable){ 
    KeyHolder keyHolder = new GeneratedKeyHolder(); 
    String sql = "INSERT INTO timetables (assignmentId, date, hoursWorked) VALUES (?, ?, ?)"; 
    template.update(new PreparedStatementCreator() { 
     public PreparedStatement createPreparedStatement(Connection con) throws SQLException { 
      PreparedStatement pst = con.prepareStatement(sql, new String[] {"id"}); 

      pst.setInt(1, timetable.getAssignmentId()); 
      pst.setDate(2, new java.sql.Date(timetable.getDate().getTime())); 
      pst.setInt(3, timetable.getHoursWorked()); 
      return pst; 
     } 
    }, keyHolder); 
    return (BigInteger) keyHolder.getKey(); 
} 
} 

Model類

package ro.database.jdbcPontaj.model; 

import javax.validation.constraints.Max; 
import javax.validation.constraints.Min; 
import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 
import java.util.Date; 

public class Timetable { 

private int timetableId; 
private int assignmentId; 
private Date date; 
private String project; 

@NotNull 
@Min(0) 
@Max(12) 
private int hoursWorked; 


public int getTimetableId() { 
    return timetableId; 
} 

public void setTimetableId(int timetableId) { 
    this.timetableId = timetableId; 
} 

public int getAssignmentId() { 
    return assignmentId; 
} 

public void setAssignmentId(int assignmentId) { 
    this.assignmentId = assignmentId; 
} 

public Date getDate() { 
    return date; 
} 

public void setDate(Date date) { 
    this.date = date; 
} 

public int getHoursWorked() { 
    return hoursWorked; 
} 

public void setHoursWorked(int hoursWorked) { 
    this.hoursWorked = hoursWorked; 
} 

public String getProject() { 
    return project; 
} 

public void setProject(String project) { 
    this.project = project; 
} 


public Timetable() { 
} 

public Timetable(int timetableId, String project, Date date, int hoursWorked) { 
    this.timetableId = timetableId; 
    this.project=project; 
    this.date = date; 
    this.hoursWorked = hoursWorked; 
} 

public Timetable(int timetableId, int assignmentId, Date date, int hoursWorked) { 
    this.timetableId = timetableId; 
    this.assignmentId = assignmentId; 
    this.date = date; 
    this.hoursWorked = hoursWorked; 
} 

} 

的Html

​​

UPDATE:

時間表(跳過引導的div)的時間表

 <div class="row"> 
      <div class="col-md-10 title"> 
       <h2>Timetable</h2> 
      </div> 
      <div class="col-md-2"> 
      </div> 
      <div class="col-md-12"> 

       <table class="table table-bordered"> 
        <thead> 
        <tr> 
         <th>id</th> 
         <th>assignment</th> 
         <th>date</th> 
         <th>number of hours</th> 
        </tr> 
        </thead> 
        <tbody> 
        <tr th:each = "timetable: ${timetables}"> 
         <td th:text="${timetable.timetableId}">45</td> 
         <td th:text="${timetable.project}">vasi</td> 
         <td th:text="${timetable.date}">1 ian</td> 
         <td th:text="${timetable.hoursWorked}">3000</td> 
        </tr> 
        </tbody> 
       </table> 

服務方法

@Autowired 
JdbcTemplate template; 

public List<Timetable> findAll(String loginname) { 
    String sql = " SELECT timetables.timetableId, timetables.assignmentId, timetables.date, " + 
      "timetables.hoursWorked, users.username, projects.projectName AS project " + 
      "FROM timetables INNER join assignments on timetables.assignmentId = assignments.assignmentId " + 
      "INNER JOIN projects on assignments.projectId = projects.projectId " + 
      "INNER JOIN users on users.userId = assignments.userId where username= ?"; 

    RowMapper<Timetable> rm = new RowMapper<Timetable>() { 
     @Override 
     public Timetable mapRow(ResultSet resultSet, int i) throws SQLException { 
      Timetable timetable = new Timetable(resultSet.getInt("timetableId"), 
        resultSet.getString("project"), 
        resultSet.getDate("date"), 
        resultSet.getInt("hoursWorked")); 

      return timetable; 
     } 
    }; 

    return template.query(sql, rm, loginname); 
} 

的時間表

@RequestMapping(value = {"/Timetable"}, method = RequestMethod.GET) 
public String index(Model md){ 
    org.springframework.security.core.Authentication auth = SecurityContextHolder.getContext().getAuthentication(); 
    String loginname = auth.getName(); 
    md.addAttribute("timetables", service.findAll(loginname)); 
    return "Timetable"; 
} 

回答

1

控制器方法,如果我理解正確你有tw一個html頁面顯示所有的分配,一個顯示你輸入的新項目。我認爲當新的輸入頁面出現驗證錯誤時,會出現錯誤。

替代這些線路

if (bindingResult.hasErrors()) { 
    return "AddToTimetable"; 
} 

與這些的

if (bindingResult.hasErrors()) { 
    return "newEntry";//replace the newentry with the html page that you enter the new entry 
} 

當出現錯誤時,你應該去的頁面,你想進入的頁面中的新條目並不算有所有的任務。

+0

重定向不是問題。時間表頁面查詢數據庫表中的所有內容。 AddToTimetable通過提交按鈕在數據庫中添加一個新條目,然後重定向到顯示所有行(包括新行)的時間表。 嘗試加載AddToTimetable時發生錯誤,我懷疑在表單標記中,它表示時間表(這是一個對象)沒有BindingResult,因爲它在控制器中有一個是假的。問題是我不知道如何解決它,因爲堆棧上的其他類似問題具有不同的結構。 – Adi

+0

@Adi返回到「AddToTimetable」,它以哪種形式出現?「重定向:/時間表」,它以何種形式出現? –

+0

AddToTimetable是添加新條目的表單。如果你問這個地址不知道,因爲這個錯誤我無法測試。 時間表是一個簡單的自舉表。地址是localhost:8080 /時間表我會用時間表更新OP – Adi

相關問題