Editing xhtml jsp files in Eclipse

2019-04-14 04:38发布

问题:

I've got some jsp files that are supposed to output xhtml. They seem to have the correct doctype etc but Eclipse is not parsing the xhtml attributes. For instance for the root element:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

I get the warning: "Undefined attribute name (xmlns)." What's the best way to solve this in Eclipse?

edit: The doctype for this particular file was wrong apparently, it was set to:

<!DOCTYPE html PUBLIC 
   "-//W3C//DTD XHTML 1.1 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Which does not exist obviously. Eclipse is now complaining about taglib tags though, i.e.:

<%@taglib prefix="s" uri="/struts-tags" %>

generates the warning: "Tag (jsp:directive.taglib) should be an empty-element tag."

回答1:

That seems odd, I use the same in Eclipse but with PHP and it works fine.

What is the DOCTYPE that you use? I've used

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">



回答2:

If you're going to ouput xml (in my understanding xhtml is xml) then you should be using the jsp document syntax, for instance your

<%@taglib prefix="s" uri="/struts-tags" %>

should instead be a namespace in some top-level tag. For the project I'm working on all the jsp are like this

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root version="2.0"
      xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:c="http://java.sun.com/jsp/jstl/core"
      xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
      xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<jsp:directive.page language="java"
    contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"/>
<jsp:text><![CDATA[<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">]]>
</jsp:text>
<html xmlns="http://www.w3.org/1999/xhtml">
...
</html>
</jsp:root>


回答3:

If you're using JSF, check if you got the view tag twice.

That's how I solved the message: Undefined attribute name "xmlns:ui"



回答4:

Eventually, you can install JBoss Tools (update site for Eclipse, guides) which provides quite good XHTML / JSP editors.