Minor changes

This commit is contained in:
gmaipady 2015-12-15 15:52:52 +05:30
parent cd4d7beb86
commit 06ded372a4
7 changed files with 128 additions and 65 deletions

View File

@ -21,45 +21,50 @@ import org.baeldung.thymeleaf.model.Student;
@Controller
public class StudentController {
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) {
// get mock objects
List<Student> students = buildStudents();
// add current student
students.add(student);
model.addAttribute("students", students);
}
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
}
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) {
// get mock objects
List<Student> students = buildStudents();
// add current student
students.add(student);
model.addAttribute("students", students);
}
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
}
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public String addStudent(Model model) {
model.addAttribute("student", new Student());
return "addStudent";
}
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public String addStudent(Model model) {
model.addAttribute("student", new Student());
return "addStudent";
}
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) {
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) {
model.addAttribute("students", buildStudents());
model.addAttribute("students", buildStudents());
return "listStudents";
}
return "listStudents";
}
private List<Student> buildStudents() {
List<Student> students = new ArrayList<Student>();
private List<Student> buildStudents() {
List<Student> students = new ArrayList<Student>();
Student student1 = new Student();
student1.setId(1001);
student1.setName("John Smith");
students.add(student1);
Student student1 = new Student();
student1.setId(1001);
student1.setName("John Smith");
student1.setGender('M');
student1.setPercentage(Float.valueOf("80.45"));
Student student2 = new Student();
student2.setId(1002);
student2.setName("Jane Williams");
students.add(student2);
students.add(student1);
return students;
}
Student student2 = new Student();
student2.setId(1002);
student2.setName("Jane Williams");
student2.setGender('F');
student2.setPercentage(Float.valueOf("60.25"));
students.add(student2);
return students;
}
}

View File

@ -7,34 +7,54 @@ import javax.validation.constraints.NotNull;
/**
*
* Simple student POJO with two fields - id and name
* Simple student POJO with few fields
*
*/
public class Student implements Serializable {
private static final long serialVersionUID = -8582553475226281591L;
private static final long serialVersionUID = -8582553475226281591L;
@NotNull(message = "Student ID is required.")
@Min(value = 1000, message = "Student ID must be atleast 4 digits.")
private Integer id;
@NotNull(message = "Student ID is required.")
@Min(value = 1000, message = "Student ID must be atleast 4 digits.")
private Integer id;
@NotNull(message = "Student Name is required.")
private String name;
@NotNull(message = "Student name is required.")
private String name;
public Integer getId() {
return id;
}
@NotNull(message = "Student gender is required.")
private Character gender;
public void setId(Integer id) {
this.id = id;
}
private Float percentage;
public String getName() {
return name;
}
public Integer getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Character getGender() {
return gender;
}
public void setGender(Character gender) {
this.gender = gender;
}
public Float getPercentage() {
return percentage;
}
public void setPercentage(Float percentage) {
this.percentage = percentage;
}
}

View File

@ -1,4 +1,9 @@
msg.id=ID
msg.name=Name
msg.gender=Gender
msg.percent=Percentage
welcome.message=Welcome Student !!!
msg.AddStudent=Add Student
msg.ListStudents=List Students
msg.Home=Home

View File

@ -26,7 +26,7 @@
<!-- Message source -->
<beans:bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<beans:property name="basename" value="messages"></beans:property>
<beans:property name="basename" value="messages" />
</beans:bean>
<!-- Resolves views selected for rendering by @Controllers to .html resources
@ -41,11 +41,11 @@
<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
<beans:property name="templateResolver" ref="templateResolver" />
</beans:bean>
<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<beans:property name="templateEngine" ref="templateEngine" />
<beans:property name="order" value="1" />
<beans:property name="viewNames" value="*"></beans:property>
<beans:property name="viewNames" value="*" />
</beans:bean>
<context:component-scan base-package="org.baeldung.thymeleaf.controller" />

View File

@ -11,16 +11,29 @@
<ul>
<li th:errors="*{id}" />
<li th:errors="*{name}" />
<li th:errors="*{gender}" />
<li th:errors="*{percentage}" />
</ul>
<table border="1">
<tr>
<td><label th:text="#{msg.id}"></label></td>
<td><label th:text="#{msg.id}" /></td>
<td><input type="number" th:field="*{id}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.name}"></label></td>
<td><label th:text="#{msg.name}" /></td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.gender}" /></td>
<td><select th:field="*{gender}">
<option th:value="'M'" th:text="Male"></option>
<option th:value="'F'" th:text="Female"></option>
</select></td>
</tr>
<tr>
<td><label th:text="#{msg.percent}" /></td>
<td><input type="text" th:field="*{percentage}" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>

View File

@ -6,10 +6,18 @@
</head>
<body>
<h1>
<span th:text="#{welcome.message}"></span>
<span th:text="#{welcome.message}" />
</h1>
<P>
Current time is <span th:text="${serverTime}"></span>
</P>
<p>
Current time is <span th:text="${serverTime}" />
</p>
<table>
<tr>
<td><a th:href="@{/addStudent}" th:text="#{msg.AddStudent}" /></td>
</tr>
<tr>
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
</tr>
</table>
</body>
</html>

View File

@ -9,16 +9,28 @@
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}"></th>
<th th:text="#{msg.name}"></th>
<th th:text="#{msg.id}" />
<th th:text="#{msg.name}" />
<th th:text="#{msg.gender}" />
<th th:text="#{msg.percent}" />
</tr>
</thead>
<tbody>
<tr th:each="student: ${students}">
<td th:text="${#conversions.convert(student.id,'Float')}"></td>
<td th:text="${{student.name}}"></td>
<td th:text="${student.id}" />
<td th:text="${{student.name}}" />
<td th:switch="${student.gender}"><span th:case="'M'"
th:text="Male" /> <span th:case="'F'" th:text="Female" /></td>
<td th:text="${#conversions.convert(student.percentage, 'Integer')}" />
</tr>
</tbody>
</table>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}" />
</p>
</div>
</body>
</html>