Commit 7f0b359f by mrf00020

Ejemplo prof no funciona

parent 155d4c0e
...@@ -55,6 +55,11 @@ ...@@ -55,6 +55,11 @@
<artifactId>tomcat-embed-jasper</artifactId> <artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.example.fmaker;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "employees")
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {
}
public Employee(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "email_address", nullable = false)
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId +
"]";
}
}
\ No newline at end of file
package com.example.fmaker;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import com.example.fmaker.Employee;
import com.example.fmaker.EmployeeRepository;
@Controller
public class EmployeeController {
@Autowired
private EmployeeRepository employeeRepository;
@GetMapping("/")
public String index(Model model) {
return "index";
}
@GetMapping("/showEmployees")
public ModelAndView showCities() {
List < Employee > employees = employeeRepository.findAll();
Map < String, Object > params = new HashMap < String, Object > ();
params.put("employees", employees);
return new ModelAndView("showEmployees", params);
}
}
package com.example.fmaker;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.fmaker.Employee;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
\ No newline at end of file
package com.example.fmaker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FmakerApplication {
public static void main(String[] args) {
SpringApplication.run(FmakerApplication.class, args);
}
}
package com.example.fmaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.List;
import com.example.fmaker.Employee;
import com.example.fmaker.EmployeeRepository;
@SpringBootApplication
public class MainAplication implements CommandLineRunner {
@Autowired
private EmployeeRepository repository;
public static void main(String[] args) {
SpringApplication.run(MainAplication.class, args);
}
@Override
public void run(String...args) throws Exception {
repository.save(new Employee("Ramesh", "Fadatare", "ramesh@gmail.com"));
repository.save(new Employee("Tom", "Cruise", "tom@gmail.com"));
repository.save(new Employee("John", "Cena", "john@gmail.com"));
repository.save(new Employee("tony", "stark", "stark@gmail.com"));
// get list of employees
List < Employee > employees = repository.findAll();
employees.forEach(employee -> System.out.println(employee.toString()));
}
}
spring.freemarker.template-loader-path: classhpath:/templates
spring.freemarker.suffix: .ftl
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Home Page</h2>
</div>
<a href="showEmployees">Show Employees (Retrieve employee data from database)</a>
</div>
</div>
</body>
</html>
showEmployees.ftl
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Employee List</h2>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<#list employees as employee>
<tr>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
<td>${employee.emailId}</td>
</tr>
</#list>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<title>Employee List</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading">
<h2>Employee List</h2>
</div>
<div class="panel-body">
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<#list employees as employee>
<tr>
<td>${employee.firstName}</td>
<td>${employee.lastName}</td>
<td>${employee.emailId}</td>
</tr>
</#list>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
\ No newline at end of file
<html>
<head>
<title> ${title} </title>
</head>
<body>
<h1>${title}</h1>
<p>${exampleObject.name} by ${exampleObject.developer}</p>
<ul>
<#list systems as system>
<li>${system_index + 1}. ${system.name} from ${system.developer}</li>
</#list>
</ul>
</body>
</html>
\ No newline at end of file
...@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test; ...@@ -4,7 +4,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class FmakerApplicationTests { class MainAplicationTests {
@Test @Test
void contextLoads() { void contextLoads() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment