first struts2 project

今天第一次用struts2开发,还真是跟struts1有好多不同,一个hello word也做了好久。现在把需要注意的点标记一下。

1.struts2的配置文件(struts.xml)是放在web-inf/classes 目录下;struts1是放在web-inf目录下。

2.struts2的标签引用是<%@ taglib uri="/struts-tags" prefix="s" %>,而且他只有这种<s:></s> 种标签;struts1的标签引用

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

有bean,html,logic,tiles四种

3.中文乱码问题,如果是post提交只需在struts.xml配置   <constant name="struts.i18n.encoding" value="UTF-8"></constant>  

   如果是get提交则有问题,需要编码转换:username=new String(username.getBytes("iso-8859-1"),"UTF-8");  原因是显而易见的:

 

这个原因是服务器导致的,服务器比如tomcat接收get默认使用ISO-8859-1编码,而浏览器发送时文字编码页面编码是和页

面保持致的,如果页面使用utf-8 编码 get方法文字自使用utf-8编码,但接收服务器没设置情况用ISO-8859-1编码接收,

中文自乱码由于ISO-8859-1编码单字节编码所以我们可以使用getBytes("ISO-8859-1"),"utf-8"这样把文字重新转换utf-8

编码。
第一个发送的文字是utf-8编码而tomcat接收用错误ISO-8859-1编码getBytes("ISO-8859-1")后会重新得到正确的utf-8编

码字节数组,然后用 new String(request.getParameter("something").getBytes("ISO-8859-1"),"utf-8")重新将字节编

码成UTF-8编码的文字文字这样文字就正确了。
如果tomcat的话server.xml文件里<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" 

redirectPort="8443" URIEncoding="UTF-8"/>后边的URIEncoding就是设置get编码的如果没有指定url接收的编码类型,

会自动用ISO-8859-1编码

 

4.struts.xml include new xml file的时候报错:

   struts.xml :

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="myFirst" namespace="/" extends="struts-default">
<action name="login" class="com.asm.LoginAction">
<result name="loginSuccess">/success.jsp</result>
<result name="loginFailure">/failure.jsp</result>
</action>
</package>


<include file="test.xml"></include>


</struts>   


 test.xml file:

<struts>
<package name="jack" namespace="/jack" extends="struts-default">
<action name="test4" class="com.asm.Test4Action">
<result name="success">/test4.jsp</result>
</action>
</package>
</struts>


报错信息:

严重: Document is invalid: no grammar found. at (null:2:8)
org.xml.sax.SAXParseException: Document is invalid: no grammar found.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:250)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:626)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3095)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:921)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)

  

   原因是inlude的test.xml格式错误:

  要加上一句:

  <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="jack" namespace="/jack" extends="struts-default">
<action name="test4" class="com.asm.Test4Action">
<result name="success">/test4.jsp</result>
</action>
</package>
</struts>

 

你可能感兴趣的:(struts2,url)