Struts JSP 页面显示不正确,在浏览器中显示未解析的源代码

Posted

技术标签:

【中文标题】Struts JSP 页面显示不正确,在浏览器中显示未解析的源代码【英文标题】:Struts JSP page does not display correctly, it shows the unparsed source code in browser 【发布时间】:2016-04-27 20:49:15 【问题描述】:

我使用 Tomcat 8 和 Struts 1.3.10

当我执行我的应用程序时

http://127.0.0.1:8080/appli/welcome.jsp

我的浏览器里有这个

"> ">

控制台消息:

2016-01-21 15:20:39 调试 ModuleUtils:171 - 获取路径的模块名称 /generic/authentification/Login.do 2016-01-21 15:20:39 调试 ModuleUtils:196 - 找到的模块名称:默认 2016-01-21 15:20:39 DEBUG RequestProcessor:161 - 处理路径的“GET” '/generic/authentification/Login' 2016-01-21 15:20:39 DEBUG TilesRequestProcessor:265 - uri=/WEB-INF/jsp/generic/tiles/template_blank.jsp doInclude=false 2016-01-21 15:25:12 DEBUG ApplicationFilter:59 - 请求的路径: '/accueil.jsp' - 方法:GET 2016-01-21 15:25:12 DEBUG ApplicationFilter:63 - 访问被拒绝 2016-01-21 15:25:12 DEBUG ModuleUtils:171 - 获取路径的模块名称 /generic/authentification/Login.do 2016-01-21 15:25:12 调试 ModuleUtils:196 - 找到的模块名称:默认 2016-01-21 15:25:12 DEBUG RequestProcessor:161 - 处理路径的“GET” '/generic/authentification/Login' 2016-01-21 15:25:12 调试 TilesRequestProcessor:265 - uri=/WEB-INF/jsp/generic/tiles/template_blank.jsp doInclude=false

怎么了?我忘记配置了 有些东西?

我的 Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.project</groupId>
    <artifactId>appli</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>appli</name>

    <dependencies>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Applicatif -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-core</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-extras</artifactId>
            <version>1.3.10</version>
        </dependency>

        <!-- JSP TLD -->
        <dependency>
            <groupId>net.fckeditor</groupId>
            <artifactId>java-core</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> 
            <version>1.1.2</version> </dependency> -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-taglib</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-el</artifactId>
            <version>1.3.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts-tiles</artifactId>
            <version>1.3.10</version>
        </dependency>

        <!-- Birt -->
        <dependency>
            <groupId>org.eclipse.birt.runtime</groupId>
            <artifactId>org.eclipse.birt.runtime</artifactId>
            <version>4.3.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>org.apache.poi</artifactId>
                    <groupId>org.eclipse.birt.runtime</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <!-- Divers -->

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>com.novell.ldap</groupId>
            <artifactId>jldap</artifactId>
            <version>2009-10-07</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.5</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>apex</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <archive>
                        <manifestEntries>
                            <Specification-Title>J2EE Servlet</Specification-Title>
                            <Specification-Version>2.5</Specification-Version>
                            <Specification-Vendor>Sun Microsystems</Specification-Vendor>
                            <Implementation-Title>$pom.name</Implementation-Title>
                            <Implementation-Version>$pom.version</Implementation-Version>
                            <Implementation-Vendor>$pom.organization.name</Implementation-Vendor>
                            <Built-By>me</Built-By>
                            <Implementation-URL>$pom.url</Implementation-URL>
                        </manifestEntries>
                    </archive>
                    <compilerArguments>
                        <endorseddirs>$endorsed.dir</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>$endorsed.dir</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

welcome.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!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=ISO-8859-1">
<title>Login Example</title>
</head>
<body>
    <html:form action="/login" focus="userName">
Username : <html:text property="userName" />
        <br>
Password : <html:password property="password" />
        <br>
        <html:submit value="login" />
    </html:form>
</body>
</html>

web.xml

当我评论这两行时,一切正常,但是当我取消评论这一行时,我有 pb

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
    <display-name>Struts Blank Application</display-name>

    <!-- Standard Action Servlet Configuration -->
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <!-- <init-param> <param-name>chainConfig</param-name> <param-value>org/apache/struts/tiles/chain-config.xml</param-value> 
            </init-param> -->
        <load-on-startup>2</load-on-startup>
    </servlet>


    <!-- Standard Action Servlet Mapping -->
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
<!--    <servlet-mapping> -->
<!--        <servlet-name>jsp</servlet-name> -->
<!--        <url-pattern>/javascript/generic/messages.js</url-pattern> -->
<!--    </servlet-mapping> -->
<!--    <servlet-mapping> -->
<!--        <servlet-name>jsp</servlet-name> -->
<!--        <url-pattern>*.css</url-pattern> -->
<!--    </servlet-mapping> -->

    <!-- The Usual Welcome File List -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

【问题讨论】:

尝试发布你的welcome.jsp @Abdelhak 我已经发布了我的 jsp 问题表明 JSP 引擎根本没有运行。这反过来表明您在web.xml 中以某种方式覆盖了它。例如,通过在/* 的 URL 模式上错误地映射不同的 servlet。 【参考方案1】:

如果你在jsp页面中有未解析的代码意味着

JSP 引擎根本没有运行

您已覆盖 web.xml 中的 Web 配置。因为你没有发布它的问题我不能告诉你哪里错了。可能是您错误地将action servlet 映射到/*。这只是标准配置

  <!-- Standard Action Servlet Configuration -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
 </servlet>


  <!-- Standard Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

【讨论】:

我在 web.xml 中添加了解释 删除这些行。将js和css文件映射到jsp是废话。

以上是关于Struts JSP 页面显示不正确,在浏览器中显示未解析的源代码的主要内容,如果未能解决你的问题,请参考以下文章

请问怎么在jsp上实现页面跳转但是浏览器的url不变,或者怎么配置struts2

web-inf下的jsp怎么访问

Struts2标记中的HTML标记

如何在 Struts 2 中将数据库记录列表(通过 Hibernate 检索)显示到 JSP 页面?

从一个简单的小实例分析JSP+Servelt与JSP+Struts2框架的区别

使用struts mvc在jsp中显示动态值而不使用scriptlet