我的CSS停止了工作

时间:2015-04-01 17:21:05

标签: java jsp servlets

我的css文件有问题,我暂时无法解决。我在java网络技术方面相对较新,我不知道是什么导致了这一点。我正在尝试编写一个基于MVC的应用程序,其中我的servlet作为控制器工作,jsp作为视图,我的数据库是我的模型。我的HomeController现在应该从数据库中检索类别的名称并将其转发到视图(home.jsp)。它工作正常,我可以在我的视图中显示类别但是因为我已经实现了HomeController,我的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&amp;Sell - online auctions.</title>
</head>

stylesheet.css放在WebContent文件夹中。

提前致谢。

编辑: CSS文件从一开始就在同一个文件夹中。

1 个答案:

答案 0 :(得分:2)

您已将您的servlet映射到/。这是一个映射,意味着:每个请求都进入servlet。因此,当浏览器向/stylesheet.css发送请求时,请求将转到servlet,而不是由提供静态文件的默认容器提供的servlet处理。

将您的servlet映射到另一个URL。

相关问题