Assignment10
Assignment10
OUTPUT:
List .
addnews.
list after addnews save.
Codes.
Home.java
package com.wipro.newslatest.controller;
import java.util.List;
import com.wipro.newslatest.service.*;
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.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import com.wipro.newslatest.dto.News;
@Controller
public class Home {
@Autowired
NewsService newsService;
@GetMapping("/greet")
String greet()
{
System.out.println("Greet");
return "hello";
}
@GetMapping("/addnews")
String news()
{
System.out.println("Addnews");
return "addnews";
}
@PostMapping("/save")
public String save(@ModelAttribute News news) {
newsService.save(news);
return"addnews";
}
@GetMapping("/list")
public String listnews(Model m)
{
List<News> listNews =newsService.findAll();
System.out.println("--List News--"+listNews);
m.addAttribute("list", listNews);
return "list";
}
}
News.java
package com.wipro.newslatest.dto;
NewsRepository.java
package com.wipro.newslatest.repo;
import java.util.*;
import org.springframework.stereotype.Repository;
import java.util.List;
import com.wipro.newslatest.dto.News;
@Repository
public NewsRepository() {
super();
newslist=new ArrayList<>();
newslist.add(n);
newslist.add(n1);
newslist.add(n11);
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.wipro.newslatest.repo.*;
@Service
public class NewsService {
@Autowired
NewsRepository newsRepository;
public List<News> findAll()
{
return newsRepository.findAll();
}
public void save(News news) {
newsRepository.save(news);
}
addnews.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
href="<a
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstra
p.min.css%22"
rel="nofollow"><u>https://cdn.jsdelivr.net/npm/[email protected]/dist/
css/bootstrap.min.css"</u></a>
integrity="sha384-
T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<form method="post" action="/save" modelAttibute="news">
<h2>Add User</h2>
<div class="form-group">
<label for="id">Id:</label> <input type="text"
class="form-control" id="id"
type="number" name="id" placeholder="id">
</div>
<div class="form-group">
<label
for="newscategory">NewsCategory:</label> <input type="text"
class="form-control" id="newscategory"
name="newscategory" placeholder="Enter Category">
</div>
<div class="form-group">
<label
for="newsdescription">NewsDescription:</label> <input type="text"
class="form-control" id="newsdescription"
name="newsdescription" placeholder="Enter Description">
</div>
list.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.library.controller.*"%>
<%@ page import="com.library.dao.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.lang.*" %>
<%@ page import="java.util.Date" %>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-
8859-1">
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>News List</title>
</head>
<body>
<H1> List of News </H1>
<ul>
</ul>
</body>
</html>
Application.properties
spring.application.name=newslatest
server.port=9000
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wipro</groupId>
<artifactId>newslatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>newslatest</name>
<description>News Application</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet.jsp.jstl</groupId>
<artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>jakarta.servlet.jsp.jstl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-
plugin</artifactId>
</plugin>
</plugins>
</build>
</project>