I've made an application using `Spring Boot http://localhost:8080/vehicleApp, the contents of my JSP file are getting printed rather than the expected output.
Below is the code:
VehicleApplication.java (main class)
package com.Vehicle.prototype;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class VehicleApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleApplication.class, args);
System.out.println("Vehicle Application started");
}
}
MvcConfig.java
package com.vehicle.prototype.config;
#Configuration
#EnableWebMvc
public class MvcConfig extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
VehicleController.java
package com.vehicle.prototype.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class VehicleController {
#RequestMapping(value="vehicleApp")
public String home(Model model) {
model.addAttribute("greetings", "hello");
return "vehicleApp";
}
}
src/main/webapp/WEB-INF/jsp/vehicleApp.jsp
(These same contents are getting printed as it is)
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<!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>JSP Page</title>
</head>
<body>
<h1>${greetings}</h1>
</body>
</html>
Please let me know what am I missing?
Thank You
Related
So I was following a guide on how to create a Spring MVC, but an issue I'm running into is that it seems to not display the template literal values correctly.
Here's the guide and the code:
https://www.youtube.com/watch?v=7ArHUCL_RRc
https://github.com/RameshMF/spring-mvc-tutorial/tree/master/springmvc5-helloworld-exmaple
Essentially, I'm following it one to one, creating a model, controller, and config that should display the message and the current date and time. However, it does not display template literals correctly, showing them as literally ${helloWorld.message}. I'm not sure if it's an issue of the helloWorld not being created, or of it's something else. Any tips?
helloworld.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Spring 5 MVC - Hello World Example | javaguides.net</title>
</head>
<body>
<h2>${helloWorld.message}</h2>
<h4>Server date time is : ${helloWorld.dateTime}</h4>
</body>
</html>
HelloWorldController.java
package net.te549.springmvc.controller;
import java.time.LocalDateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.ui.Model;
import net.te549.springmvc.model.HelloWorld;
#Controller
public class HelloWorldController {
#RequestMapping("/helloworld")
public String handler(Model model) {
HelloWorld helloWorld = new HelloWorld();
helloWorld.setMessage("Hello World from TE549!!!");
helloWorld.setDateTime(LocalDateTime.now().toString());
model.addAttribute("helloWorld", helloWorld);
return "helloworld";
}
}
HelloWorld.java
package net.te549.springmvc.model;
public class HelloWorld {
private String message;
private String dateTime;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message=message;
}
public String getDateTime() {
return dateTime;
}
public void setDateTime(String dateTime) {
this.dateTime=dateTime;
}
}
(If I need to show more code, let me know)
Kindly Check if there is any errors/exception in logs if not Try Adding this in your JSP:
<%#page isELIgnored="false"%>
I am neither using ResponseBody nor I am using RestController annotation Still my Spring Application is returning String instead of jsp/html pages.
Here are my files of Application Configuration and Controller.
Where am I going wrong?
The GIT link to my code
Web Configuration File:
package com.springimplant.mvc.config;
import java.io.IOException;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.ResourceBundleViewResolver;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.springframework.web.servlet.view.XmlViewResolver;
#Configuration
#EnableWebMvc
#ComponentScan(basePackages="com.springimplant.mvc.controllers")
public class SimpleWebConfiguration implements WebMvcConfigurer {
#Bean
public ViewResolver internalResourceViewResolver() {
// UrlBasedViewResolver bean = new UrlBasedViewResolver();
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/views/");
bean.setSuffix(".jsp");
bean.setOrder(0);
return bean;
}
#Bean
public ViewResolver resourceBundleViewResolver() {
ResourceBundleViewResolver bean = new ResourceBundleViewResolver();
bean.setBasename("views");
bean.setOrder(1);
return bean;
}
#Bean
public ViewResolver xmlViewResolver(){
XmlViewResolver bean = new XmlViewResolver();
bean.setLocation(new ClassPathResource("views.xml"));
bean.setOrder(2);
return bean;
}
#Override
public void configureViewResolvers(ViewResolverRegistry registry) {
registry.jsp("/WEB-INF/views/", ".jsp");
}
#Override
public void addViewControllers(ViewControllerRegistry registry) {
// registry.addViewController("/").setViewName("forward:/welcome");
}
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
HomeController:
package com.springimplant.mvc.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
#Controller
#RequestMapping("/")
public class HomeController {
#RequestMapping(value="welcome",method=RequestMethod.GET)
public ModelAndView welcome()
{
return new ModelAndView("welcome");
}
}
<%# 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>Welcome Home</title>
</head>
<body>
Welcome Home
</body>
</html>
So I Had these page tag over my jsp file which I removed and now it runs fine but why?
I didn't had to do this for my earlier projects.
I am learning spring Boot framework right now, I am trying to apply i18n concept on my simple application. But whenever I run the application, the following error returned: "No message found under code 'label.welcomeMessage' for locale 'en_US'.". I have read about this issue and tried a lot before I getting here to ask but noting have worked with me.
Here is my AppClass configuration:
package com.abed.main.configuration;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
#Configuration
public class AppConfig implements WebMvcConfigurer{
#Bean
public LocaleResolver localeResolver()
{
SessionLocaleResolver slr = new SessionLocaleResolver();
slr.setDefaultLocale(Locale.US);
return slr ;
}
#Bean
public LocaleChangeInterceptor LocaleChangeInterceptor()
{
LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
lci.setParamName("lang");
return lci ;
}
{
}
#Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(LocaleChangeInterceptor());
}
#Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource msgSrc = new ReloadableResourceBundleMessageSource();
msgSrc.setBasename("classpath:messages/ticket");
msgSrc.setDefaultEncoding("UTF-8");
return msgSrc;
}
}
Here is the welcome JSP:
<%# 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 = "spring" uri="http://www.springframework.org/tags" %>
<!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><spring:message code="label.welcomePageTitle"></spring:message></title>
</head>
<body>
<h1><spring:message code="label.welcomeMessage"></spring:message></h1>
<form action="ticket" method="GET">
<spring:message code="label.ticketId"></spring:message><input type="text" name="Student_Id">
<input type="submit" value="<spring:message code="label.search"></spring:message>">
</form>
<spring:message code="label.createTicketSubmit"></spring:message>
</body>
</html>
and here is the hierarchy of my application:
Any Help Please , Thanks in advance
You need to configuration a bean named messageSource
here is the code, add it to a java code file, make sure #Configuration can be scan:
#Configuration
public class MessageSourceConfig {
#Bean(name = "messageSource")
public ResourceBundleMessageSource getMessageSource() throws Exception {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setDefaultEncoding("UTF-8");
resourceBundleMessageSource.setBasenames("i18n/messages");
return resourceBundleMessageSource;
}
}
The point is : resourceBundleMessageSource.setBasenames("i18n/messages");
remember the base path is classpath, so the i18n properties resource should set in i18n/messages. In order not to crash problem, I suggest you create the properties by using IDEA.
Quote Spring official doc:
Strategy interface for resolving messages, with support for the parameterization and internationalization of such messages.
Spring provides two out-of-the-box implementations for production:
ResourceBundleMessageSource, built on top of the standard ResourceBundle
ReloadableResourceBundleMessageSource, being able to reload message definitions without restarting the VM.
This is the reason we need to configure a bean named messageSource.
I use spring boot to do a simple web page.
Here are the codes:
src/main/java/Application.java
#SpringBootApplication
public class Application
{
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
}
src/mian/java/Config.java
#Configuration
#EnableWebMvc
public class Config extends WebMvcConfigurerAdapter
{
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
{
configurer.enable();
}
#Bean
public InternalResourceViewResolver viewResolver()
{
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
src/main/java/TestController.java
#Controller
public class TestController
{
String name = "Peter!";
#RequestMapping("/test")
public String admin(ModelAndView mv)
{
System.out.println("in controller");
mv.addObject("name", name);
return "test";
}
}
src/main/webapp/WEB-INF/views/test.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
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>Just for test</title>
</head>
<body>
${name}
</body>
</html>
I start the project from the Application.java,
and put http://localhost:8080/test in the browser,
but the browser just show the source code of test.jsp.
It seems spring boot treat jsp as txt file...
How to fix it?
add this dependency to your pom.xml
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#page import="com.studytrails.tutorials.springremotingrmiclient.TestSpringRemotingRmi" %>
<!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>Insert title here</title>
</head>
<body bgcolor="red">
<h1><font color=blue>login successfull</font></h1>
<h2><font color="lightgreen">
<form action="${pageContext.request.contextPath}/MyServlet" method="post">
<input type=submit name="n1" value="Click here to play the video">
</form>
</h2>
</body>
</html>
Need to run the below program when clicking the button in jsp file.
package com.studytrails.tutorials.springremotingrmiclient;
import java.io.*;
import java.io.File.*;
import java.net.Socket;
import java.net.UnknownHostException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.Resource;
import com.studytrails.tutorials.springremotingrmiserver.*;
public class TestSpringRemotingRmi
{
public static void main(String[] args) throws IOException
{
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config-client.xml");
GreetingService greetingService = (GreetingService)context.getBean("greetingService");
String greetingMessage = greetingService.getGreeting("Alpha");
System.out.println("The greeting message is : " + greetingMessage);
greetingService.getText();
}
}
In above program I am trying to run java TestSpringRemotingRmi class in the time of button click performed in jsp file. MyServlet file contains the code for process the request from jsp file. but I try to call the default constructor of that call using jsp but it does not work. guide me to solve this problem.