2014-11-06 102 views
3

我有一個GlassFish 3.1.2應用服務器,並且我已經開始使用JSP頁面,所以我有興趣爲UTF-8正確配置編碼。什麼是標準web.xml java頭獲得<jsp-config>標籤工作?

我原來的工作web.xml文件開始

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd"> 

但後來我添加了下面將其強制UTF-8編碼

<jsp-config> 
    <jsp-property-group> 
     <url-pattern>*.jsp</url-pattern> 
     <page-encoding>UTF-8</page-encoding> 
    </jsp-property-group> 
</jsp-config> 

和我的GlassFish 3.1.2服務器日誌文件中報告

Element type <jsp-config> must be declared web.xml 

爲了解決這個錯誤,我改變了我的012的開始文件是

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
version="2.4" > 

,現在我得到一個GlassFish服務器錯誤

Invalid Deployment Descriptors in Deployment descriptor file WEB-INF/web.xml ... 
One of '{"http://java.sun.com/xml/ns/j2ee":servlet-class, 
     "http://java.sun.com/xml/ns/j2ee":jsp-file}' is expected. 

是我的語法了嗎?如何獲取<jsp-config>版本2.3的xml.web文件中的標題?

更新1

如果我開始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" xmlns:web="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_2_3.xsd" 
    version="2.3"> 

<display-name>myApp</display-name> 
<description>My Application</description> 

<listener> 
    <listener-class>...</listener-class> 
</listener> 

<servlet> 
    <servlet-name>MessageBrokerServlet</servlet-name> 
    <display-name>MessageBrokerServlet</display-name> 
    <servlet-class>...</servlet-class> 
    <init-param> 
     <param-name>...</param-name> 
     <param-value>...</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet> 
    <servlet-name>Reg</servlet-name> 
    <servlet-class>com.mydomain.servlet.Reg</servlet-class> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Reg</servlet-name> 
    <url-pattern>/reg</url-pattern> 
</servlet-mapping> 

<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
</welcome-file-list> 

<jsp-config> 
    <jsp-property-group> 
     <url-pattern>*.jsp</url-pattern> 
     <page-encoding>UTF-8</page-encoding> 
    </jsp-property-group> 
</jsp-config> 

</web-app> 

我看到這個錯誤:

[#|2014-11-06T13:03:59.779-0800|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=1;_ThreadName=Thread-2;|DPL8015: 

Invalid Deployment Descriptors in Deployment descriptor file WEB-INF/web.xml in archive [myapp]. Line 7 Column 41 -- s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'var _U = "undefined"; var g_HttpRelativeWebRoot = "/ocom/";'.|#]

[#|2014-11-06T13:03:59.779-0800|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=1;_ThreadName=Thread-2;|DPL8005: Deployment Descriptor parsing failure : s4s-elt-character: Non-whitespace characters are not allowed in schema elements other than 'xs:appinfo' and 'xs:documentation'. Saw 'var _U = "undefined"; var g_HttpRelativeWebRoot = "/ocom/";'.|#]

更新2

另外,我也有一個glassfish-web.xml文件在WEB-INF目錄下。如果我將web.xml文件還原爲原始文件,並將<jsp-config>部分置於glassfish-web.xml而不是web.xml,則服務器啓動正常。哪個文件是<jsp-config>應該進入,還是這很重要?

回答

3

要使用UTF-8配置JSP你需要使用,在JSP的第一行:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> 

你應該使用這個,但你的版本:

<?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" xmlns:web="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" 
    version="3.0"> 

的例子我的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" xmlns:web="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" 
    version="3.0"> 
    <display-name>base</display-name> 
    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/spring/spring-mvc-dispatcher.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/spring/spring-database.xml, 
      /WEB-INF/spring/spring-security.xml 
     </param-value> 
    </context-param> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
</web-app> 
+0

謝謝@stel,我在哪裏可以找到正確的語法與版本2.3使用? – user46688 2014-11-06 20:57:26

+0

@ user46688將_3_0.xsd和version =「3.0」更改爲_2_3.xsd和version =「2.3」 – Arturo 2014-11-06 21:01:29

+0

感謝@stel,但發生這些更改時出現此錯誤:'非空白字符不允許在模式元素之外'xs:appinf o'和'xs:documentation'。鋸'var _U =「undefined」;' – user46688 2014-11-06 21:08:39

0

對於3.1的servlet API級別使用:

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

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
    version="3.1" 
    metadata-complete="true">