我的css文件有问题,我暂时无法解决。我在java web技术方面比较新,我不知道会导致什么。我试图编写一个基于MVC的应用程序,其中我的servlet作为控制器工作,jsp作为视图,而我的数据库是我的模型。我的HomeController现在应该从数据库中检索类别的名称并将其转发给视图(home.jsp)。它工作正常,我可以在我的视图中显示类别,但自实施HomeController后,我的css文件已停止工作。所以我结束了工作控制器和awfull视图。我不知道问题出在哪里。我的CSS停止工作
这里有显著文件:
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<context-param>
<param-name>databaseURL</param-name>
<param-value>jdbc:postgresql://localhost:5432/buyandsell</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>admin</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value>admin</param-value>
</context-param>
<servlet>
<servlet-name>HomeController</servlet-name>
<servlet-class>controllers.HomeController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
的HomeController:
public class HomeController extends HttpServlet {
private static final long serialVersionUID = 1L;
private String databaseURL;
private String username;
private String password;
/**
* @see HttpServlet#HttpServlet()
*/
public HomeController() {
super();
}
@Override
public void init(ServletConfig config) throws ServletException {
// Retrieve the database-URL, username, password from webapp init parameters
super.init(config);
ServletContext context = config.getServletContext();
databaseURL = context.getInitParameter("databaseURL");
username = context.getInitParameter("username");
password = context.getInitParameter("password");
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
RequestDispatcher rd = null;
ArrayList<Category> categories = null;
try {
// Indicating proper database driver
Class.forName("org.postgresql.Driver");
// Creating a database connection object
conn = DriverManager.getConnection(databaseURL, username, password);
// Creating a statement object inside our connection object
stmt = conn.createStatement();
// Fetching a categories names
categories = fetchCategories(stmt);
// Setting categories for the view to display
request.setAttribute("categories", categories);
rd = request.getRequestDispatcher("/home.jsp");
rd.forward(request, response);
} catch(ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(stmt != null) stmt.close();
if(conn != null) conn.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
/**
* Method responsible for fetching categories names from database.
* @param Reference to statement object.
* @return Returns object which contains names of available categories in array list or null if SQLException was thrown.
*/
private ArrayList<Category> fetchCategories(Statement stmt) {
ArrayList<Category> categories = new ArrayList<Category>();
String query = "SELECT * FROM categories";
try {
// Retrieving data from defined query
ResultSet rset = stmt.executeQuery(query);
// Creating category objects which will be forwarded to the view
while(rset.next()) {
int id = rset.getInt(1);
String name = rset.getString(2);
int parentId = rset.getInt(3);
categories.add(new Category(id, name, parentId));
}
return categories;
} catch(SQLException e) {
e.printStackTrace();
}
return null;
}
}
和片段针对home.jsp的:
<%@ page import="java.util.ArrayList" %>
<%@ page import="models.Category" %>
<ul>
<%
ArrayList<Category> categories = (ArrayList<Category>) request.getAttribute("categories");
if(categories != null && !categories.isEmpty()) {
for(int i=0; i<categories.size(); i++) {
%>
<li><a href="category?id=<%= categories.get(i).getId() %>"><span><%= categories.get(i).getName() %></span></a>
<%
}
}
%>
</ul>
</div>
和:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
<title>Buy&Sell - online auctions.</title>
</head>
stylesheet.css被放置在WebContent文件夹中。
在此先感谢。
编辑:自从一开始,CSS文件就在同一个文件夹中。
您是否验证过如果您_remove_您的HomeController CSS再次开始工作? – DavidS 2015-04-01 17:31:27
我现在有。谢谢。 – 2015-04-01 17:53:06