1 Write A Spring Program For Dependency Injection by Using Constructor With Tag. Slip 1
1 Write A Spring Program For Dependency Injection by Using Constructor With Tag. Slip 1
@Component
public class Employee {
@Value("John Doe")
private String name;
// Getters
public String getName() {
return name;
}
Output:-
The application will display the message "Hello, World!" on the
hello.jsp page.
4 Write a spring MVC program for exception handling.
ExceptionHandler.java
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
@ControllerAdvice >> public class ExceptionHandler {
@ExceptionHandler(UserNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public ModelAndView
handleUserNotFoundException(UserNotFoundException exception) {
ModelAndView modelAndView = new ModelAndView("error");
modelAndView.addObject("errorMessage",
exception.getMessage());
return modelAndView; > } > @ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ModelAndView handleException(Exception exception) {
ModelAndView modelAndView = new ModelAndView("error");
modelAndView.addObject("errorMessage", "An unexpected
error occurred");
return modelAndView; >> } >> }
UserController.java
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.PathVariable;
@Controller >> public class UserController {
@Autowired >> private UserService userService;
@GetMapping("/users/{id}")
public String getUser(@PathVariable Long id, Model model) {
try { >> User user = userService.getUser(id);
model.addAttribute("user", user); >> return "user";
} catch (UserNotFoundException exception) {
throw exception; >> } >> } >> }
UserService.java import org.springframework.stereotype.Service;
@Service >> public class UserService {
public User getUser(Long id) throws UserNotFoundException {
// Simulate a database query >> if (id == 1) {
return new User(id, "John Doe"); >> } else {
throw new UserNotFoundException("User not found with id " + id);
} >> } >> }
UserNotFoundException.java public class UserNotFoundException
extends Exception {
public UserNotFoundException(String message) {
super(message); >> } >> }
dispatcher-servlet.xml >> XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/bea
ns
http://www.springframework.org/schema/beans/spring-
beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-
mvc.xsd">
<!-- Enable MVC annotation support -->
<mvc:annotation-driven/>
<!-- Define view resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewR
esolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean> >> <!-- Define exception handler -->
<bean id="exceptionHandler"
class="com.example.ExceptionHandler"/>
</beans> >> error.jsp >> Jsp
<%@ page contentType="text/html;charset=UTF-8" language="java"
%> >> <html> >> <head> >> <title>Error Page</title>
</head> >> <body> >> <h1>Error</h1>
<p>${errorMessage}</p> >> </body> >></html>