项目是在使用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
服务,一切都应该正常。