JSP page being displayed as Java Spring text

Asked

Viewed 267 times

2

My JSP page is being displayed as text.
I’m using Spring and Spring Security.
When I open the link for my browser page it opens as text (All HTML appears written on the screen as if I had opened the page in the same notepad) and does not render the JSP page.

It is all very simple because it is a test and although it is injecting the class ClienteService I don’t really use it.

This is my Controller:

@Controller
public class ClienteController{
    @Autowired ClienteService clienteService;

    @RequestMapping("/index")
        ModelAndView teste1(){
    return new ModelAndView("index");
}  
}

My JSP is this:

<%@ page language="java" contentType="text/html; charset=UTF-8"        pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE HTML>
<html lang="pt">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Teste</title>
</head>
<body>
    <div> Teste </div>
</body>
</html>  

My Spring Security Configuration File:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .inMemoryAuthentication()
            .withUser("admin").password("admin").roles("USER")/*Cria usuario admin*/
            .and()
            .withUser("erro").password("erro").roles("ERRO");/*Cria usuario erro*/
}

protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
            .antMatchers("/,/home,/index").permitAll()/*Permite a todos os usuarios acessar essas paginas*/
            .antMatchers("/erro**").hasRole("ERRO")/*Permite apenas ao usuario erro acessar essaS páginaS que começam com o nome erro*/
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
            .anyRequest().authenticated()
            .and()
        .formLogin();
}

My WEB XML is this, but in reality I just copied it from another application and fixed the addresses to test:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

<context-param>
    <param-name>contextClass</param-name>
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>br.com.solucoes.painel.PainelApplication</param-value>
</context-param>

<filter>
    <filter-name>CharacterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>   

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value></param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file></welcome-file>
</welcome-file-list>

This is my application.properties:

#JSP
spring.view.prefix: /views/
spring.view.suffix: .jsp

#web
spring.mvc.date-format: yyyy-MM-dd

#porta
server.port: 9100

I don’t know if Spring security needs any additional configuration. Could someone help me?

1 answer

2

Guys, thanks to everyone who read the question and tried to help, I discovered the problem. Were JSP-related dependencies that were not in the Maven POM follow below:

<!-- for JSPs -->
    <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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.