我敢肯定这是一种非常凌乱的处理方式,但是:
我有一个Maps地图,尝试以Viewable的形式返回数据,以便在使用JSP的用户列表前端的上下文中使用。
但是,在我的JSP文件中没有通过所述Maps进行迭代,因此没有任何结果。
这是我的代码
JSP
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%--
Document : all-users
Created on : Nov 22, 2018, 5:13:55 AM
Author : cubsy
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style><%@include file="css/users-page.css"%></style>
<title>Users</title>
</head>
<body>
<!-- screen top -->
<div class="header">
<h1>Cubapp</h1>
<p>A new twitter style forum service</p>
</div>
<div class="navbar">
<a href="#">My Account</a>
<a href="#">All Accounts</a>
<a href="#">Messages</a>
<a href="#" class="right">Sign Out</a>
</div>
<!-- screen top -->
<div class="row">
<div class="side"></div>
<div class="main">
<!-- This is where the user boxes will go inside the "main" flex field -->
<c:forEach items="${map}" var="user">
<p>Key: ${user.key}</p>
<div class="card">
<div class="userProfile">
<p>User</p>
</div>
<div class="container">
<h4><b>${user.username}</b></h4>
<p>${user.firstname} ${user.lastname}</p>
</div>
</div>
</c:forEach>
</div>
</div>
</body>
</html>
UserResource类(处理网络呼叫)
package fish.payara.cubappmessenger.resource;
import fish.payara.cubappmessenger.base.User;
import fish.payara.cubappmessenger.service.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import org.glassfish.jersey.server.mvc.Viewable;
@Path("users")
public class UserResource {
UserService uService = new UserService();
List<String> quotes = new ArrayList<>();
@GET
@Path("/signup")
public Viewable loginPage() {
Map<String, String> quote = new HashMap<>();
String q = "Welcome Home Lads!";
quote.put("quote", q);
return new Viewable("/signup", quote);
}
@GET
@Path("/{userId}")
public Viewable getUserById(@PathParam("userId") String id) {
Map<String, String> userInfo = new HashMap<>();
userInfo.put("username", uService.getUserById(id).getUsername());
userInfo.put("firstname", uService.getUserById(id).getFirstName());
userInfo.put("lastname", uService.getUserById(id).getLastName());
return new Viewable("/test", userInfo);
}
@GET
public Viewable getUsers() {
//List<Map<String, String>> u = new ArrayList<>();
Map<String, Map<String, String>> map = new HashMap<>();
for (int i=0; i<uService.getAllUsers().size(); i++) {
Map<String, String> newUser = new HashMap<>();
User user = uService.getAllUsers().get(i);
newUser.put("username", user.getUsername());
newUser.put("firstname", user.getFirstName());
newUser.put("lastname", user.getLastName());
map.put(user.getUsername(), newUser);
}
return new Viewable("/all-users", map);
}
@POST
@Path("/newuser")
public Response addNewUser(@FormParam("username") String username,
@FormParam("firstname") String fname,
@FormParam("lastname") String sname,
@Context UriInfo uriInfo) {
UriBuilder builder = uriInfo.getBaseUriBuilder();
builder.path("/users/"+username);
User u = new User(username, fname, sname);
uService.addUser(u);
return Response.seeOther(builder.build()).build();
}
}
UserService类(处理与数据库的连接-连接确实与对与数据库进行交互的其他元素的测试已经并且仍然成功)
package fish.payara.cubappmessenger.service;
import fish.payara.cubappmessenger.base.Message;
import fish.payara.cubappmessenger.base.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserService {
Connection con = null;
public UserService() {
String url, username, password;
url = "jdbc:mysql://localhost:3306/Cubapp";
username = "root";
password = "Huskyanna234";
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println(e);
}
}
//===============User Handling===============
public List<User> getAllUsers() {
List<User> users = new ArrayList<>();
String sql = "select * from Users";
try {
//Statement statement = con.createStatement();
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
User u = new User();
u.setUsername(rs.getString("username"));
u.setFirstName(rs.getString("firstName"));
u.setLastName(rs.getString("lastName"));
users.add(u);
}
} catch (Exception e) {
System.out.println(e);
}
return users;
}
public User getUserById(String id) {
User u = new User();
String sql = "select * from Users where username='"+id+"'";
try {
// Statement statement = con.createStatement();
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);
if(rs.next()) {
u.setUsername(rs.getString("username"));
u.setFirstName(rs.getString("firstName"));
u.setLastName(rs.getString("lastName"));
}
} catch (Exception e) {
System.out.println(e);
}
return u;
}
public User addUser(User user) {
String sql = "insert into Users values (?,?,?)";
try {
//PreparedStatement statement = con.prepareStatement(sql);
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, user.getUsername());
statement.setString(2, user.getFirstName());
statement.setString(3, user.getLastName());
statement.executeUpdate();
} catch (Exception e) {
System.out.println(e);
}
return user;
}
//=================User Handling=================
}
我想我刚刚犯了一个愚蠢的语法错误或某些错误,我是基于Java的Web开发的新手,但任何帮助将不胜感激。