我有一个report.jsp页面,如下所示(注意:我刚刚添加了必要的代码)。

<form name="report" action="../printOrganization" method="post">
<table>
    <tr>
        <td>
            Organization name: <input type="text" name="orgName" />
        </td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Submit" name="action" />
        </td>
    </tr>
</table>




当用户单击report.jsp页面的“提交”按钮时。该请求被发送到称为OrganizationServlet的servlet。并且请求由doPost方法处理。 OrganizationServlet中的代码如下所示:

public class OrganizationServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String organizatio = request.getParameter("orgName");

    if (organizatio.equals("ABC")) {
        printAllOrganization();

    }
}

public void printAllOrganization()
{
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
    pw.println("<html>\n");
    pw.println("<head>\n");
    pw.println("<title> Print Organization </title>\n");
    pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"../styles/myStyle.css\">\n"); // This style sheet doesn't show effect when program run in browser
    pw.println("</head>\n");

    pw.println("<body>\n");

        //printing all organization code is here!

    pw.println("</body>\n");


    pw.println("</html>\n");
    pw.close();
}


web.xml中处理请求的部分如下所示:

<servlet>
  <servlet-name>servletForOrganization</servlet-name>
  <servlet-class>com.project.report.OrganizationServlet</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>servletForOrganization</servlet-name>
  <url-pattern>/printOrganization</url-pattern>
</servlet-mapping>


我的css文件位于以下路径中:

MyProject > resource > styles > myStyle.css


当我在浏览器中运行应用程序时,OrganizationServlet servlet的printAllOrganization()方法中的css样式没有显示任何效果。有人可以帮我如何解决这个问题。先感谢您。

最佳答案

添加您的家庭JSP文件:

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

  <link rel="stylesheet" href="<c:url value="/resource/styles/myStyle.css" />">


添加dispatcher-servlet.xml:

  <mvc:resources mapping="/resource/**" location="/resource/" />


修改您的课程:

 public class OrganizationServlet extends HttpServlet {

        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

            String organizatio = request.getParameter("orgName");

            if (organizatio.equals("ABC")) {
                printAllOrganization(request);

            }
        }

        public void printAllOrganization(HttpServletRequest request) {
            PrintWriter pw = response.getWriter();
            response.setContentType("text/html");

            pw.println("<!DOCTYPE html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n");
            pw.println("<html>\n");
            pw.println("<head>\n");
            pw.println("<title> Print Organization </title>\n");
            pw.println("<link rel=\"stylesheet\" type=\"text/css\" HREF=\"" + request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/resource/styles/myStyle.css\">\n");
            pw.println("</head>\n");

            pw.println("<body>\n");

            //printing all organization code is here!
            pw.println("</body>\n");

            pw.println("</html>\n");
            pw.close();
        }

09-28 11:28