javaspring-bootspring-mvcjdb

Model.addAttribute not passing any thing to the .jsp


Dependency:

<?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.5.4-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.eecs.a3.teamafk</groupId>
    <artifactId>MLS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>MLS</name>
    <description>MLS DEMO</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>9.0.40</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.4-SNAPSHOT</version>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

/**
 * Display the inside owner search result
 * @param model the id for searching owner
 */
@GetMapping(value = "/owner/display")
public String ownerdisplay(Model model,@RequestParam(value = "id", required = true) int id) {
    model.addAttribute("onwer", ownerLookup(id));
    return "view-owner";
}

Spring-boot is running fine Verified that both methods works fine:

View name 'view-owner', model {onwer=org.eecs.a3.teamafk.MLS.Owner@6f7189d5, org.springframework.validation.BindingResult.onwer=org.springframework.validation.BeanPropertyBindingResult: 0 errors}
Forwarding to [/WEB-INF/jsp/view-owner.jsp]

after forwarding to the jsp, nothing shows up, not even String or anything.

/**
 * The actual method for searching owner
 * @param id the id for searching owner
 */

public Owner ownerLookup(int id) {
    try {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("Connecting...");
        conn = DriverManager.getConnection(DB_URL,USER,PASS);
        System.out.println("Succeeded!");
        stmt = conn.createStatement();
        sql = "SELECT * FROM owner WHERE id="+id;
        ResultSet rs = stmt.executeQuery(sql);
        rs.next();
        if(rs==null){
            return null;
        }
            String firstname = rs.getString("firstname");
            String lastname = rs.getString("lastname");
            String phone = rs.getString("phone");
            String email = rs.getString("email");
            Owner owner = new Owner.Builder().firstnameOfOwner(firstname).lastnameOfOwner(lastname).phoneNumber(phone).emailAddress(email).build();
            return owner;
    } catch (SQLException throwables) {
        throwables.printStackTrace();
    }
    return null;
}

I did not use web.xml, but I have tried adding it to WEB-INF folder with newer servlet version, also tried turn off EL ignore.

Here is my JSP

`This is the JSP I am using, the page show up normally, but with no value in it.

<%@page contentType="text/html;charset=UTF-8" language="java" %>    <%@page isELIgnored="false" %> <html>    <head>
       <title>View Books</title>    </head>    <body>
       <table>
           <thead>
               <tr>
                   <th>FirstName: </th>
                   <th>LastName: </th>
                   <th>Phone: </th>
                   <th>Email: </th>
               </tr>
           </thead>
           <tbody>
                   <tr>
                       <td>${owner.firstname}</td>
                       <td>${owner.lastname}</td>
                       <td>${owner.phone}</td>
                       <td>${owner.email}</td>
                   </tr>
           </tbody>
       </table>    </body> </html>

`


Solution

  • May be typo on "onwer" instead of "owner" while setting in model attribute. Check in JSP page whether it is the same variable as model attribute.

    I suspect that this is the case.

    Update : As JSP code is added in the question it is clear that the model attribute variable name had typo.

    Change

    model.addAttribute("onwer", ownerLookup(id));
    

    To

    model.addAttribute("owner", ownerLookup(id)); //owner spelling correct