Project使用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;
}
});
}
返回&#34; HomeController.java&#34;它的实现如下..
@RequestMapping("/adminHome")
public ModelAndView adminHome(User user){
List<User> list= user.getUsers();
return new ModelAndView("adminHome","list",list);
}
,相应的JSP是&#34; adminHome.jsp&#34;,如下所示......
<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;
}
答案 0 :(得分:1)
您没有将其正确地重定向到/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
服务,一切都应该正常。