gpt4 book ai didi

java - 数据库详细信息未在 Spring-MVC 中显示

转载 作者:行者123 更新时间:2023-11-29 07:37:05 25 4
gpt4 key购买 nike

项目在使用 MySql 的 Java Spring MVC 上。显示不工作(下面附有图片),因为可能函数没有返回查询结果,或者可能是其他原因。以下是用于对数据库运行查询并返回结果的函数。此函数是“User.java”文件的成员。

public List<User> getUsers()
{
return jdbcTemplate.query("select * from student",new RowMapper<User>()
{
public User mapRow(ResultSet rs, int row) throws SQLException
{
User u = new User();
u.setUserName(rs.getString(1));
u.setPassword(rs.getString(2));
u.setName(rs.getString(3));
u.setBranch(rs.getString(4));
u.setM1(rs.getInt(5));
u.setM2(rs.getInt(6));
u.setM3(rs.getInt(7));
u.setAggregate(rs.getInt(8));
u.setGrade(rs.getString(9));
return u;
}
});
}

这返回到“HomeController.java”,它的实现如下..

@RequestMapping("/adminHome")  
public ModelAndView adminHome(User user){
List<User> list= user.getUsers();
return new ModelAndView("adminHome","list",list);
}

对应的JSP是“adminHome.jsp”,如下图...

<body>
<h3>Hi ${name}</h3>
<h1>USERS List</h1>
<table border="2" width="70%" cellpadding="2">
<tr><th>Username</th><th>Password</th><th>Name</th><th>Mobile</th>
<th>Branch</th><th>Marks1</th><th>Marks2</th><th>Marks3</th>
<th>Aggregate</th><th>Grade</th><th>Edit</th>
<th>Delete</th></tr>
<c:forEach var="u" items="${list}">
<tr>
<td>${u.username}</td>
<td>${u.password}</td>
<td>${u.name}</td>
<td>${u.mobile}</td>
<td>${u.branch}</td>
<td>${u.marks1}</td>
<td>${u.marks2}</td>
<td>${u.marks3}</td>
<td>${u.aggregate}</td>
<td>${u.grade}</td>
<td><a href="editemp/${u.username}">Edit</a></td>
<td><a href="deleteemp/${u.username}">Delete</a></td>
</tr>
</c:forEach>
</table>
<br/>
<a href="register">Add New User</a>
</body>

This is the final output and the list of users is not returning

@RequestMapping(value = "/user", method = RequestMethod.POST)
public String user(@Validated User user, Model model,Locale locale)
{
U = user.getUserName();
String s1 = "password";
String s2 = "name";
String p = "X";
try
{
final String selectQuery = "select * from student where username='"+U+"'";
Map<?, ?> map = jdbcTemplate.queryForMap(selectQuery);
Set set = map.entrySet();
Iterator itr=set.iterator();

while(itr.hasNext())
{
//Converting to Map.Entry so that we can get key and value separately
Map.Entry entry=(Map.Entry)itr.next();
if(entry.getKey().equals(s1))
{
p = entry.getValue().toString();
System.out.println(p);
}
if(entry.getKey().equals(s2))
{
NAME = entry.getValue().toString();
System.out.println(p);
}
}
}
catch(Exception e2)
{
System.out.println("Exception Raised while Login(user)!!!"); //not inclusion of case of NULL return by username search in the sql query
}

String dest="user";
if(user.getUserName().equals("admin") && user.getPassword().equals(p))
{
dest = "adminHome";
System.out.println("Admin Page Requested");
model.addAttribute("name", NAME);
}
else if(user.getUserName().equals(U) && user.getPassword().equals(p))
{
System.out.println("User Page Requested");
model.addAttribute("name", NAME);
}
else
{
dest = "errLogin"; //redirects to errLogin.jsp
System.out.println("Err Login Page Requested, locale = " + locale);
Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
String formattedDate = dateFormat.format(date);

model.addAttribute("serverTime", formattedDate);
model.addAttribute("message", "Invalid login credentials !!! Try Again");
}
return dest;
}

最佳答案

您没有将其正确重定向到 /adminHome服务。相反,它重定向到 adminHome.jsp页。

如果你想重定向到/adminHome来自 /user服务,而不是 return "adminHome"; ,执行以下操作 -

@RequestMapping(value = "/user", method = RequestMethod.POST)
public String user(@Validated User user, Model model,Locale locale) {
// your code and use return line as follows
return "redirect:adminHome";
}

当你使用 String dest对于您的最终返回声明,请将其更新为 "redirect:<service_name>" .

那应该叫你的 /adminHome服务和一切都应该工作正常。

关于java - 数据库详细信息未在 Spring-MVC 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48275213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com