Trouble Getting Started with Spring Session

45 Views Asked by At

I'm working on creating a prototype web app in Java on a JBoss server. I'm using Spring, and right now I'm trying to get an understanding of session management but I'm having difficulty getting even the most basic form to work on JBoss.

I'm following this guide https://www.javainuse.com/spring/springboot_session

I couldn't find a guide that matched my exact situation. JBoss, and Oracle. My oracle connection is declared as a Datasource in the JBoss console. This example guide has you instead setup your DB credentials in the application.properties, I had to change that. I also had some issues with importing some of the libraries in pom.xml so I tweaked that too.

I'm here cause the code doesn't seem to be running, and also I'm not getting any meaningfull errors, just blank pages.

The code should render a form at root / You can enter a value and post that to /persistMessage which will add that as a session var and then redirect you back to root / where the value you entered will be displayed via the session

When I load up root I get the form, enter value, submit and /persistMessage just loads a white page, does not redirect. I did some troubleshooting and it seems to be failing on the very first line of code "List messages = (List) request.getSession().getAttribute("MY_SESSION_MESSAGES");"

Can anyone point me in the right direction to start troubleshooting this? Or, perhaps point me to a better guide or educational material that fits my scenario?

I'm waiting on database credentials, as soon as I get those I'll log in and see if the spring_session table has been created. ( I assume it wont be)

My Setup:

I have the following libraries:

Prototype1Application.java

package private.prototype1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;    

@SpringBootApplication
public class Prototype1Application {

    public static void main(String[] args) {
        SpringApplication.run(Prototype1Application.class, args);
    }

}

ServerInitializer.java

package private.prototype1;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Prototype1Application.class);
    }

}

SpringSessionController.java

package private.prototype1;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SpringSessionController {


    @GetMapping("/")
    public String process(Model model, HttpSession session) {
        @SuppressWarnings("unchecked")
        List<String> messages = (List<String>) session.getAttribute("MY_SESSION_MESSAGES");

        if (messages == null) {
            messages = new ArrayList<>();
        }
        model.addAttribute("sessionMessages", messages);

        return "index";
    }
    

    @PostMapping("/persistMessage")
    public String persistMessage(@RequestParam("msg") String msg, HttpServletRequest request) {
        //@SuppressWarnings("unchecked")
        List<String> messages = (List<String>) request.getSession().getAttribute("MY_SESSION_MESSAGES");
        
        if (messages == null) {
            messages = new ArrayList<>();
            request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        }
        messages.add(msg);
        request.getSession().setAttribute("MY_SESSION_MESSAGES", messages);
        return "redirect:/";
    }
    
    
    @PostMapping("/destroy")
    public String destroySession(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/";
    }
}

templates/index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Spring Boot Session Example</title>
</head>
<body>
    <div>
        <form th:action="@{/persistMessage}" method="post">
            <textarea name="msg" cols="40" rows="2"></textarea>
            <br> <input type="submit" value="Save Message" />
        </form>
    </div>
    <div>
        <h2>Messages2</h2>
        <ul th:each="message : ${sessionMessages}">
            <li th:text="">msg</li>
        </ul>
    </div>
    <div>
        <form th:action="@{/destroy}" method="post">
            <input type="submit" value="Destroy Session" />
        </form>
    </div>
</body>
</html>

application.properties This is where I departed from the guide, I jused the jndi-name to point to my jboss datasource. I did this based on some googling around.. not sure this is the correct way.

spring.datasource.jndi-name = java:jboss/OracleDSprottype

spring.h2.console.enabled=true

spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=always
spring.session.timeout.seconds=900

pom.xml I also departed from the guide here, I removed the mysql library since i was using oracle. Not sure if I should have replaced it with something else.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.11</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>private</groupId>
<artifactId>prototype-1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>prototype-1</name>
<description>prototype for a spring site</description>
<properties>
    <java.version>11</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <exclusions>
        <!-- ************************************************************* -->
        <!-- had to exclude these items or the War wouldnt deploy on JBoss -->
        <!-- ************************************************************* -->
            <exclusion>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>com.oracle.database.jdbc</groupId>
        <artifactId>ojdbc8</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.logging</groupId>
        <artifactId>jboss-logging</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>       
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-jdbc</artifactId>
    </dependency>
</dependencies>
<build>
    <plugins>
    </plugins>
</build>
0

There are 0 best solutions below