0% found this document useful (0 votes)
3 views

Assignment10

Uploaded by

Reddy Veerendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Assignment10

Uploaded by

Reddy Veerendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name : Aryan Shukla Resume id:26179642

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;

public class News {


int Id;
String Newscategory;
String Newsdescription;

public News(int id, String newscategory, String


newsdescription) {
super();
Id = id;
Newscategory = newscategory;
Newsdescription = newsdescription;
}
public int getId() {
return Id;
}
public void setId(int id) {
this.Id = id;
}
public String getNewscategory() {
return Newscategory;
}
public void setNewscategory(String newscategory) {
Newscategory = newscategory;
}
public String getNewsdescription() {
return Newsdescription;
}
public void setNewsdescription(String newsdescription) {
Newsdescription = newsdescription;
}
@Override
public String toString() {
return "News [Id=" + Id + ", Newscategory=" +
Newscategory + ", Newsdescription=" + Newsdescription + "]";
}

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 class NewsRepository {


List<News> newslist;

public NewsRepository() {
super();
newslist=new ArrayList<>();

News n=new News(1,"Education","Board Results Out");


News n1=new News(2,"Sports","Mumbai Indians won the
Match");
News n11=new News(3,"Stocks","Sensex going for Bull");

newslist.add(n);
newslist.add(n1);
newslist.add(n11);

public List<News> findAll()


{
return newslist;
}
public void save(News news)
{newslist.add(news);}
}
NewsService.java
package com.wipro.newslatest.service;
import com.wipro.newslatest.dto.News;
import com.wipro.newslatest.repo.*;

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>

<button type="submit" class="btn btn-


primary">Save</button>
</form>
</div>
</body>
</html>

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>

<c:forEach var="news" items="${list}" >


<li>
${news.id}- ${news.newscategory}- ${news.newsdescription}
</li>
</c:forEach>

</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>

You might also like