I am trying to run Spring MVC application which handles a simple user details save functionality from jsp to database and displaying the records on screen.
Technology stack used
OPEN JDK 8(Amazon Corretto)
Websphere Liberty Server 20.0
eclipse Oxygen
When I am trying to execute the code it is displaying index.jsp but from index.jsp. I have 2 links in index.jsp add user and list user when user clicks on adduser or list user it is throwing "JSPG0036E: failed to find the resourcejsp" error.
Can anyone please help.
Herewith attached the code block...
userController.java // ControllerClass
import java.util.ArrayList;
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.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import com.demo.bean.UserBean;
import com.demo.model.User;
import com.demo.service.UserService;
@Controller
@RequestMapping(value="/hello")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value = "/save", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute("command")UserBean userBean,
BindingResult result) {
User user = prepareModel(userBean);
userService.addUser(userBean);
return new ModelAndView("redirect:/add.html");
}
@RequestMapping(value="/users", method = RequestMethod.GET)
public ModelAndView listUsers() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("users", prepareListofBean(userService.listUsers()));
return new ModelAndView("userList", model);
}
@RequestMapping(value = "/add", method = RequestMethod.GET)
public ModelAndView addUser(@ModelAttribute("command")UserBean userBean,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("Users", prepareListofBean(userService.listUsers()));
return new ModelAndView("addUser", model);
}
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView welcome() {
return new ModelAndView("index");
}
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public ModelAndView editUser(@ModelAttribute("command")UserBean userBean,
BindingResult result) {
userService.deleteUser(prepareModel(userBean));
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", null);
model.put("users", prepareListofBean(userService.listUsers()));
return new ModelAndView("addUser", model);
}
@RequestMapping(value = "/edit", method = RequestMethod.GET)
public ModelAndView deleteUser(@ModelAttribute("command")UserBean userBean,
BindingResult result) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("user", prepareUserBean(userService.getUser(userBean.getId())));
model.put("users", prepareListofBean(userService.listUsers()));
return new ModelAndView("addUser", model);
}
private User prepareModel(UserBean userBean){
User user = new User();
user.setUserId(userBean.getUserId());
user.setUserName(userBean.getUserName());
user.setUserEmail(userBean.getUserEmail());
return user;
}
private List<UserBean> prepareListofBean(List<User> user){
List<UserBean> beans = null;
if(user != null && !user.isEmpty()){
beans = new ArrayList<UserBean>();
UserBean bean = null;
for(User user1 : user){
bean = new UserBean ();
bean.setUserId(user.getUserId());
bean.setUserName(user.getUserName());
bean.setUserEmail(user.getUserEmail());
beans.add(bean);
}
}
return beans;
}
private UserBean prepareUserBean(User user){
UserBean bean = new UserBean();
bean.setUserId(user.getUserId());
bean.setUserName(user.getUserName());
bean.setUserEmail(user.getUserEmail());
return bean;
}
}
Index.jsp // Jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring3MVC with Hibernate3 CRUD Example using Annotations</title>
</head>
<body>
<h2>Spring3MVC with Hibernate3 CRUD Example using Annotations</h2>
<h2>1. <a href="users.html">List of Users</a></h2>
<h2>2. <a href="add.html">Add User</a></h2>
</body>
</html>
AddUser.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Spring MVC Form Handling</title>
</head>
<body>
<h2>Add User Data</h2>
<form:form method="POST" action="/sdnext/add.html">
<table>
<tr>
<td><form:label path="id">User ID:</form:label></td>
<td><form:input path="id" value="${user.userId}" readonly="true"/></td>
</tr>
<tr>
<td><form:label path="name">User Name:</form:label></td>
<td><form:input path="name" value="${user.userName}"/></td>
</tr>
<tr>
<td><form:label path="age">User eMail:</form:label></td>
<td><form:input path="age" value="${user.userEmail}"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit"/></td>
</tr>
</table>
</form:form>
<c:if test="${!empty users}">
<h2>List Users</h2>
<table align="left" border="1">
<tr>
<th>User ID</th>
<th>User Name</th>
<th>User Email</th>
<th>Actions on Row</th>
</tr>
<c:forEach items="${users}" var="user">
<tr>
<td><c:out value="${user.userId}"/></td>
<td><c:out value="${user.userName}"/></td>
<td><c:out value="${user.userEmail}"/></td>
<td align="center"><a href="edit.html?id=${user.userId}">Edit</a> | <a href="delete.html?id=${user.userId}">Delete</a></td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>
userController. java - мой класс контроллера. Для меня index. jsp выполняется успешно, но когда я нажимаю на кнопку добавления пользователя, появляется указанная выше ошибка «Файл не найден». Я использую Apache ANT 1.9.9 в качестве инструмента сборки. Код успешно собирается, но выдает ошибку при выполнении Jsplink, упомянутого в index. jsp.
Заранее спасибо.