模型.addAttribute不向.jsp传递任何东西



依赖关系:

<?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运行良好验证两种方法都可以正常工作:

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]

转发到jsp后,没有显示任何内容,甚至没有字符串或任何内容。

/**
* 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;
}

我没有使用web.xml,但是我已经尝试将它添加到使用较新servlet版本的WEB-INF文件夹中,也尝试关闭EL忽略。

这是我的JSP

'这是我正在使用的JSP,页面正常显示,但其中没有值。

<%@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>

可能在" onwer"不用"owner"在设置模型属性时。在JSP页面中检查它是否与模型属性相同。

我怀疑情况就是这样。

更新:由于JSP代码被添加到问题中,很明显模型属性变量名有拼写错误。

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

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

相关内容

  • 没有找到相关文章

最新更新