Error with parsing on the view? - java

I try to make an web application that will conect with a repository on a server, and i want to print the data on the screen. Actually i try to create a RESTful client. Probably there is a parsing error that doesn't allow me to see the display data
My jsp is the following:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Get Author</h1>
<c:if test="${empty author}">
No records found!
</c:if>
<c:if test="${!empty author}">
<table style="border: 1px solid #333">
<tr>
<td style="width: 100px">Id</td>
<td>${data.id}</td>
</tr>
<tr>
<td>Name</td>
<td>${data.name}</td>
</tr>
<tr>
<td>Address</td>
<td>${data.address}</td>
</tr>
</table>
</c:if>
</body>
</html>
The controller is the following(i implement the controller with two different ways. The one of them is commented)
#Controller
public class Contraller {
protected static Logger logger = Logger.getLogger("controller");
private RestTemplate restTemplate = new RestTemplate();
#RequestMapping(value = "/datas", method = RequestMethod.GET)
public String getDatas(Model model) {
HttpEntity<Data> entity = new HttpEntity<Data>(headers);
// Send the request as GET
try {
ResponseEntity<DataList> result = restTemplate.exchange("http://www../data/",
HttpMethod.GET, entity, DataList.class);
// Add to model
model.addAttribute("datas", result.getBody().getData());
} catch (Exception e) {
}
// This will resolve to /WEB-INF/jsp/personspage.jsp
return "personspage";
}
/**
* Retrieves a single record from the REST provider
* and displays the result in a JSP page
*/
#RequestMapping(value = "/data", method = RequestMethod.GET)
public String getMyData(#RequestParam("id") Long id, Model model) {
try{
Data results = restTemplate.getForObject("http://www.../data/{id}",
Data.class, id);
model.addAttribute("data", results);
}catch(Exception e){
}
return "getpage";
}
The model:
#XmlRootElement(name = "data", namespace="http://www...")
#XmlAccessorType(XmlAccessType.FIELD)
public class Data {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setFirstName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
In the repository there are 4000 datas. When i give the following URL: localhost:8080/Client_for_rest/data?id=1
(all the id's from 1 to 4000) returns me the view but it doesn't display the data. If i give an id bigger than 4000, i.e. 4001 gives me back that there are no records which is true. According to this i suppose that the client connects with the server side but there is a problem(i suppose with parsing) that it doesn't allow the data to be dispayed on the view. I'm unfamiliar with the Spring MVC framework and i read something about marshaller and castor but i don't how to implement them. Actually i was wondering if there is an easier way to solve my problem. Do i have to use pojo maven dependencies etc?

if you method return type is string use map object and bindresult as the parameters to that method.
and add elements to that map object.
you can directly access that map object on your jsp page
as an example :
#RequestMapping("/locationUpdate.do")
public String locationUpdate(Map<String, Object> map,#ModelAttribute("location") Location location, BindingResult result) {
map.put("location", locationService.getLocationByCode(locationcode));
map.put("locGrp", listLocationGroup());
return "location/locationAdd";
}
now you can directly access location and locGrp in your jsp

Related

Thymeleaf iterating through list of objects shows blank

I am trying to iterate through objects passed through ModelMap using Spring MVC. However, the text shows up blank when tested on browser. For example :
test.jsp :
<!DOCTYPE html>
<html>
<body>
Hello ${test}
<tr th:each="user,iterStat : ${testList}">
<td th:text="${user.id}" />
<td th:text="${user.name}" />
</tr>
</body>
</html>
Controller code :
#GetMapping("/test")
public String test(Model model) {
model.addAttribute("test", "Bob");
List<ChatUser> users = new ArrayList<>();
users.add(new ChatUser("asdf"));
users.add(new ChatUser("ewfasdf"));
model.addAttribute("testList", users);
return "test";
}
When loading up /test after deploying my webapp I would see Hello Bob, but nothing else. Checking the source of the webpage I see this :
<!DOCTYPE html>
<html>
<body>
Hello Bob
<tr th:each="user,iterStat : [com.mkyong.ChatUser#7a5dd809, com.mkyong.ChatUser#61f4628]">
<td th:text=""/>
<td th:text=""/>
</tr>
</body>
</html>
As seen in the th:each line, the objects are obviously being passed through properly. But the th:text is showing up just blank.
My ChatUser entity :
#Entity
public class ChatUser {
#Id
#Column
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column
private String name;
public ChatUser() {}
public ChatUser(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
What is the issue here?

can't view records stored in DB in a web page

I'm referring to this guide (http://jvmhub.com/2015/08/09/spring-boot-with-thymeleaf-tutorial-part-3-spring-data-jpa/), the only difference is that i unified the classes PostEntity and Post assuming the name PostEntity.
When i try to store data in db through a form the applications works but it can't allow me to view stored data in a webpage as showed in the guide linked above.
The web page result.html is displayed as
"Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Jul 05 14:25:42 CEST 2018
There was an unexpected error (type=Internal Server Error, status=500).
Exception evaluating SpringEL expression: "PostEntity.title" (template: "result" - line 11, col 9)"
and the console shows the exception:
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'title' cannot be found on null
It is throwed even with the other attributes (id and content) according to the order they are disposed in result.html.
If i try to print the content of records variable to the console it shows that it is not null so i can't figure out why there is this exception.
Controller:
#Controller
public class Home {
#Autowired private PostRepository postRepository;
#RequestMapping(value="/", method=RequestMethod.GET)
public String index(Model model) {
model.addAttribute("post", new PostEntity());
return "index";
}
#RequestMapping(value = "/", method = RequestMethod.POST)
public String addNewPost( PostEntity post, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "index";
}
postRepository.save(post);
List<PostEntity> records = (List<PostEntity>) postRepository.findAll();
model.addAttribute("posts", records);
return "redirect:result";
}
#RequestMapping(value = "/result", method = RequestMethod.GET)
public String showAllPosts(Model model) {
List<PostEntity> records = (List<PostEntity>) postRepository.findAll();
for (PostEntity record : records) {
System.out.println(record);
}
model.addAttribute("posts", records);
return "result";
}
}
Model class:
#Entity
public class PostEntity {
public PostEntity() {}
public PostEntity(String title, String content) {
this.title = title;
this.content = content;
}
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public int id;
public String title;
public String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
index:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf, part 2 - forms</h3>
<form action="#" th:action="#{/}" th:object="${post}" method="post">
<table>
<tr>
<td>Title:</td>
<td><input type="text" th:field="*{title}" /></td>
<td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title error message</td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" th:field="*{content}" /></td>
<td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Content error message</td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</form>
</body>
</html>
result
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf, part 3 - SPRING DATA JPA</h3>
<p th:each="PostEntity : ${posts}">
<h4>Title:</h4>
<div th:text="${PostEntity.title}"/></div>
<h4>ID:</h4>
<div th:text="${PostEntity.id}"/></div>
<h4>Content:</h4>
<div th:text="${PostEntity.content}"/></div>
<div>---------------------------------------------------------</div>
</p>
</body>
</html>
Any suggestions?
You are doing this redirect-thing. When doing this your objects added to the model doesn't reach the further process. I remember running into similar trouble.

How to display #Lob image from mysql database using spring mvc and jsp

There is link to github: https://github.com/Lukszn/ProjectProfile I'm using Spring 4.3.7.RELEASE, MySQL Connector Java: 5.1.39 and hibrnate: 5.2.9. Finaly
There i have User and his Account model. In account i have #Lob accPicture and some Strings(+ get/set). I'm trying a lot of answers from stackoverflow and documentation to show Account image, but without success. Last think what I do : created own ImageController. I successfully stored image in database but when I'm trying to display it in my jsp, it is showing "HTTP Status 400 -
The request sent by the client was syntactically incorrect."
Firstly I show you my User model:
#Entity
#Table(name = "users")
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
#Column(unique = true)
#NotBlank
#Size(min=4,max=20)
private String login;
#NotBlank
private String password;
#Column(unique = true)
#Email
#NotBlank
private String email;
private String permission;
#OneToMany()
private List<Account> accounts;
public User(final String login, final String password, final String email) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(login));
Preconditions.checkArgument(!Strings.isNullOrEmpty(password));
Preconditions.checkArgument(!Strings.isNullOrEmpty(email));
this.login = login;
this.password = password;
this.email = email;
}
public User() {
}
}
+ get/set
Account model:
#Entity
#Table(name = "accounts")
public class Account {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private boolean ifBasicAccount;
private String accTitle;
private String accFirstName;
private String accLastName;
private String accBirthdate;
private String accPhoneNumber;
private String accEducation;
private String accExperience;
private String accAbilities;
private String accInterests;
private String accProjects;
private String accDescription;
#Lob
private byte[] accPicture;
#ManyToOne
private User user;
public Account() {
}
+ get/set
next Account Controller:
#Controller
public class AccountController {
#Autowired
AccountRepository accountRepository;
#Autowired
UserRepository userRepository;
#RequestMapping(method = RequestMethod.GET, value ="addAccount")
public String addAccount(Model model) {
Account account = new Account();
model.addAttribute("account", account);
return "addAccount";
}
#RequestMapping(method = RequestMethod.POST, value ="addAccount")
public String addAccount(#ModelAttribute Account account, HttpSession session) {
User user = userRepository.findOne((Long) session.getAttribute("user_id"));
account.setIfBasicAccount(false);
account.setUser(user);
accountRepository.save(account);
return "redirect:/accounts";
}
#RequestMapping("/accounts")
public String accountList(Model model, HttpSession ses) {
long userId = (Long) ses.getAttribute("user_id");
List<Account> accounts = accountRepository.findUserAccounts(userId);
model.addAttribute("accounts", accounts);
return "accounts";
}
#RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String editAccountForm(Model model, #PathVariable long id) {
Account account = accountRepository.findOne(id);
model.addAttribute("account",account);
return "editAccountForm";
}
#RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String editAccount(#ModelAttribute Account account, #PathVariable long id) {
Account accountToUpdate = accountRepository.findOne(id);
accountToUpdate.setAccTitle(account.getAccTitle());
accountToUpdate.setAccFirstName(account.getAccFirstName());
accountToUpdate.setAccLastName(account.getAccLastName());
accountToUpdate.setAccBirthdate(account.getAccBirthdate());
accountToUpdate.setAccPhoneNumber(account.getAccPhoneNumber());
accountToUpdate.setAccEducation(account.getAccEducation());
accountToUpdate.setAccExperience(account.getAccExperience());
accountToUpdate.setAccAbilities(account.getAccAbilities());
accountToUpdate.setAccInterests(account.getAccInterests());
accountToUpdate.setAccProjects(account.getAccProjects());
accountToUpdate.setAccDescription(account.getAccDescription());
accountRepository.save(accountToUpdate);
return "redirect:/accounts";
}
#RequestMapping("/delete")
public String deleteAccount(Model model) {
return "deleteAccount";
}
#RequestMapping("/read/{id}")
public String read(#PathVariable long id) {
return accountRepository.findOne(id).toString();
}
#RequestMapping("/delete/{id}")
public String delete(#PathVariable long id) {
Account account = accountRepository.findOne(id);
accountRepository.delete(account);
return "redirect:/accounts";
}
}
and last one ImageController:
#Controller
#RequestMapping("/user")
public class ImageController {
private AccountRepository accountRepository;
#RequestMapping(value = "/accounts", method = RequestMethod.GET)
public void showImage(#RequestParam("id") Long id, HttpServletResponse response, HttpServletRequest request)
throws ServletException, IOException {
Account account = accountRepository.getOne(id);
response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
response.getOutputStream().write(account.getAccPicture());
response.getOutputStream().close();
}
}
my .jsp to show account:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%# page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# include file="/WEB-INF/parts/header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table table-striped">
<h1>Accounts:</h1>
<c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">
<tr>
<td>${theCount.index+1}</td>
<td><b>Nazwa: </b>${account.accTitle}</td>
<td><b>Opis: </b>${account.accDescription}</td>
<td><img src="/ProjectProfile/user/accounts?id=${account.id}"/></td>
<td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
<td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
</tr>
</c:forEach>
</table>
Add Account
</body>
</html>
Maybe I need use Base64Encoder, but i don't know how? .... I use pom.xml and AppConfig for configuration. Please, check out this project, maybe somebody can help?
<img id="photo" src="data:image/png;base64,${PHOTOYOUNEED}" />
In the controller responsible for the image sending to the html:
(...)
String photoencodeBase64 = modelX.getStringPhoto();
modelAndView.addObject("PHOTOYOUNEED", photoencodeBase64 );
and I also use this method in the model to convert byte[] to string in base64:
public static String convertBinImageToString(byte[] binImage) {
if(binImage!=null && binImage.length>0) {
return Base64.getEncoder().encodeToString(binImage);
}
else
return "";
}
and I call it in the getStringPhoto() getter inside the model.
Ok Eunito, let's see... Changed Account.java(model):
#Entity
#Table(name = "accounts")
public class Account {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private boolean ifBasicAccount;
private String accTitle;
private String accFirstName;
private String accLastName;
private String accBirthdate;
private String accPhoneNumber;
private String accEducation;
private String accExperience;
private String accAbilities;
private String accInterests;
private String accProjects;
private String accDescription;
#Lob
private byte[] accPicture;
private String stringPhoto;
#ManyToOne
private User user;
public Account() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getAccTitle() {
return accTitle;
}
public void setAccTitle(String accTitle) {
this.accTitle = accTitle;
}
public String getAccFirstName() {
return accFirstName;
}
public void setAccFirstName(String accFirstName) {
this.accFirstName = accFirstName;
}
public String getAccLastName() {
return accLastName;
}
public void setAccLastName(String accLastName) {
this.accLastName = accLastName;
}
public String getAccBirthdate() {
return accBirthdate;
}
public void setAccBirthdate(String accBirthdate) {
this.accBirthdate = accBirthdate;
}
public String getAccPhoneNumber() {
return accPhoneNumber;
}
public void setAccPhoneNumber(String accPhoneNumber) {
this.accPhoneNumber = accPhoneNumber;
}
public String getAccEducation() {
return accEducation;
}
public void setAccEducation(String accEducation) {
this.accEducation = accEducation;
}
public String getAccExperience() {
return accExperience;
}
public void setAccExperience(String accExperience) {
this.accExperience = accExperience;
}
public String getAccAbilities() {
return accAbilities;
}
public void setAccAbilities(String accAbilities) {
this.accAbilities = accAbilities;
}
public String getAccInterests() {
return accInterests;
}
public void setAccInterests(String accInterests) {
this.accInterests = accInterests;
}
public String getAccProjects() {
return accProjects;
}
public void setAccProjects(String accProjects) {
this.accProjects = accProjects;
}
public String getAccDescription() {
return accDescription;
}
public void setAccDescription(String accDescription) {
this.accDescription = accDescription;
}
public byte[] getAccPicture() {
return accPicture;
}
public void setAccPicture(byte[] accPicture) {
this.accPicture = accPicture;
}
public String getStringPhoto() {
return convertBinImageToString(accPicture);
}
public void setStringPhoto(String stringPhoto) {
this.stringPhoto = stringPhoto;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public boolean isIfBasicAccount() {
return ifBasicAccount;
}
public void setIfBasicAccount(boolean ifBasicAccount) {
this.ifBasicAccount = ifBasicAccount;
}
public static String convertBinImageToString(byte[] accPicture) {
if(accPicture!=null && accPicture.length>0) {
return Base64.getEncoder().encodeToString(accPicture);
}
else
return "";
}
}
I have two controllers for Account(one is only for showing image-I'm not so sure that's a good thing, because i have two the same RequestMappings). So see changed ImageController:
#Controller
#RequestMapping("/admin/user")
public class ImageController {
#Autowired
AccountRepository accountRepository;
#RequestMapping(value = "/accounts", method = RequestMethod.GET)
public void showImage(#RequestParam("id") long id, Model model) {
Account account = accountRepository.findById(id);
String photoencodeBase64 = account.getStringPhoto();
model.addAttribute("accPicture", photoencodeBase64);
}
}
And .jsp to show image:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%# page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# include file="/WEB-INF/parts/header.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div align="center">
<table class="table table-striped">
<h1>Accounts:</h1>
<c:forEach items="${accounts}" var="account" begin="0" varStatus="theCount">
<tr>
<td>${theCount.index+1}</td>
<td><b>Title: </b>${account.accTitle}</td>
<td><b>Description: </b>${account.accDescription}</td>
<td><b>Image: </b><img id="photo" src="data:image/png;base64,${account.accPicture}" /></td>
<td><a style="width: 180px;height: 20px;" href="./edit/${account.id}" class="badge badge-primary">Show/Edit</a></td>
<td><a style="width: 180px;height: 20px;" href="./delete/${account.id}" class="badge badge-danger">Delete</a></td>
</tr>
</c:forEach>
</table>
Add Account
</body>
</html>
so what is happen- when i add new account -> write Title, Name etc. and add image from file my browser show me HTTP Status 400 - The request sent by the client was syntactically incorrect. - > there i need see all user accounts.
In STS console nothing happend. In MySQL too.
Why not use Spring Content JPA? This can provide a storage service and rest endpoints for managing content associated with jpa entities.
pom.xml
<!-- Java API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-jpa</artifactId>
<version>0.1.0</version>
</dependency>
<!-- REST API -->
<dependency>
<groupId>com.github.paulcwarren</groupId>
<artifactId>spring-content-rest</artifactId>
<version>0.1.0</version>
</dependency>
Configuration
#Configuration
#EnableJpaStores
#Import("org.springframework.content.rest.config.RestConfiguration.class")
public class MysqlConfig {
// schema management
//
#Value("/org/springframework/content/jpa/schema-drop-mysql.sql")
private Resource dropRepositoryTables;
#Value("/org/springframework/content/jpa/schema-mysql.sql")
private Resource dataRepositorySchema;
#Bean
DataSourceInitializer datasourceInitializer() {
ResourceDatabasePopulator databasePopulator =
new ResourceDatabasePopulator();
databasePopulator.addScript(dropReopsitoryTables);
databasePopulator.addScript(dataReopsitorySchema);
databasePopulator.setIgnoreFailedDrops(true);
DataSourceInitializer initializer = new DataSourceInitializer();
initializer.setDataSource(dataSource());
initializer.setDatabasePopulator(databasePopulator);
return initializer;
}
}
To associate content, add Spring Content annotations to your account entity.
Account.java
#Entity
public class Account {
// replace #Lob field with
#ContentId
private String contentId;
#ContentLength
private long contentLength = 0L;
// if you have rest endpoints
#MimeType
private String mimeType = "text/plain";
Create a "store":
AccountImagesStore.java
#StoreRestResource(path="accountImages)
public interface AccountImagesStore extends ContentStore<Account, String> {
}
This is all you need to create REST endpoints # /accountImages. When your application starts, Spring Content will look at your dependencies (seeing Spring Content JPA/REST), look at your AccountImagesStore interface and inject an implementation of that interface for JPA. It will also inject a #Controller that forwards http requests to that implementation. This saves you having to implement any of this yourself whch I think is what you are after.
So...
curl -X POST /accountImages/{account-id}
with a multipart/form-data request will store the image in the database and associate it with the account entity whose id is account-id.
curl /accountImages/{account-id}
will fetch it again and so on...supports full CRUD.
So all you should need to display this in your JSP is an image tag:
There are a couple of getting started guides here. The reference guide is here. And there is a tutorial video here. The coding bit starts about 1/2 way through.
HTH

Spring MVC :how a form transfer an object to Controller?? Always 400 error

When i try to access http://localhost:8080/XX/articles/addArticle
and submit the form, there is always a "400 BAD REQUEST" error.
i've tried to look up for the reason, all i got is that object transfered from the form is not as same type as my model(, which is an Article object? here). However, i don't think i really get it..
All codes are here, the config is all good.
Here are 2 models:
Article.java
#Entity
#Table(name="article_inf")
public class Article {
private int articleId;
private String title;
private User author;
private String content;
public Article() {
}
public Article(String title, User author, String content) {
this.title = title;
this.author = author;
this.content = content;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public int getArticleId() {
return articleId;
}
public void setArticleId(int articleId) {
this.articleId = articleId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
#ManyToOne(targetEntity=User.class)
#JoinColumn(name="author", referencedColumnName="userId", nullable=false)
public User getAuthor() {
return author;
}
public void setAuthor(User author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
User.java
#Entity
#Table(name="agri_user_inf")
public class User {
private int userId;
private String userName;
private String password;
private String cellPhone;
private List<Article> articles;
public User() {
articles = new ArrayList<>();
}
public User(String userName, String password, String cellPhone) {
this.userName = userName;
this.password = password;
this.cellPhone = cellPhone;
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCellPhone() {
return cellPhone;
}
public void setCellPhone(String cellPhone) {
this.cellPhone = cellPhone;
}
#OneToMany(targetEntity=Article.class, mappedBy="author")
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
controller
ArticleController.java
#Controller
#RequestMapping("articles")
public class ArticleController {
private ArticleDao articleDao;
#Autowired
public ArticleController(ArticleDao articleDao) {
this.articleDao = articleDao;
}
#RequestMapping(value="addArticle", method=GET)
public String addArticle(ModelMap modelMap) {
List<User> authors = userDao.getAllUsers();
// add all authors
modelMap.addAttribute("authors", authors);
return "articles/addArticleForm";
}
#RequestMapping(value="addArticle", method=POST)
public String addArticle(Article article) {
articleDao.addArticle(article);
return "redirect:/articles";
}
// other code
my form addArticleForm.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post">
title: <input type="text" name="title"/><br/>
author: <select name="author">
<c:forEach items="${authors}" var="author">
<option value="${author}">${author.userName}</option>
</c:forEach>
</select>
<br/>
content: <input type="text" name="content"/><br/>
<input type="submit" value="add"/>
</form>
</body>
</html>
You are violating REST principles. Always use version in your endpoint and resource name after that. Example - /api/v1/articles. After that with help of HttpMethods access your resources. Example - If you want to
1.1 add new Article , use POST request to /api/v1/articles
1.2 delete existing Article, use DELETE request to /api/v1/articles/{articleId}
1.3 get one Article, use GET request to /api/v1/articles/{articleId}
1.4 get all Articles, use GET request to /api/v1/articles
1.5 update existing Article, use PUT request to /api/v1/articles/{articleId}
Never use your Entity which is going to be persisted in DB for all layers. It`s bad practice to connect Entity with your view, instead you can use DTO.
Use #ModelAttribute annotation in your controller layer with same name as in view to handle incoming Article object. Example
public String addArticle(#ModelAttribute("article") Article article )
To add new Article first you need to create endpoint which is returning empty Article object inside of ModelMap. Then you must handle this in your front end(JSP) and for submitting this form follow step 3.
Hope this will help.
I got solution :
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="post">
title: <input type="text" name="title"/><br/>
author: <select name="author.userId">
<c:forEach items="${authors}" var="author">
<option value="${author.userId}">${author.userName}</option>
</c:forEach>
</select>
<br/>
content: <input type="text" name="content"/><br/>
<input type="submit" value="add"/>
</form>
</body>
</html>
change name of <select> tag from "author" to "author.userId" . That works.

Why the class can use undefined members in JSTL?

I am reading JSP tutorial from a book and meeting a program that is hard to understand.
It has two beans, one is Message.java, another is MessageServies.java as below.
package com.jeecourse.model;
public class Message {
private String name;
private String text;
public Message() {
}
public Message(String name, String text) {
this.name = name;
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
And the MessageService.java
package com.jeecourse.model;
public class MessageService {
private Message[] fakeMessages;
public MessageService() {
fakeMessages = new Message[3];
fakeMessages[0] = new Message("Jimmy", "Jimmy's message!");
fakeMessages[1] = new Message("Jack", "Jack's message!");
fakeMessages[2] = new Message("Tom", "Tom's message!");
}
public Message[] getMessages() {
return fakeMessages;
}
}
And finnally the message.jsp with EL:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="messageService" class="com.jeecourse.model.MessageService"/>
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8">
<title>NoteBook</title>
</head>
<body>
<table style="text-align: left; width: 100%;" border="1">
<tr>
<td>Name</td><td>Message</td>
</tr>
<c: forEach var="message" items="${messageService.messages}">
<tr>
<td>${message.name}</td><td>${message.text}</td>
</tr>
</c: forEach>
</table>
</body>
</html>
Please note here it uses messageService.messages in EL expression. It is very strange that messageService have neither such members, nor such functions. But it can work. Why?
When you write ${messageService.messages} it gets translated at compile time to messageService.getMessages. Just in the same way that ${message.text} is invoking actually message.getText().
For this kind of "magic" it is important to follow some conventions when naming your methods. If not, the compiler won't know which method it should call when you use the abbreviated version.
You can see more about EL here: https://stackoverflow.com/tags/el/info
This feature is at the top of the page.

Categories