來處理編碼
但是當使用Get方法時,Tomcat沒有用正確的編碼來處理querystring,資料就會變成亂碼。
解決方法是修改tomcat資料夾下的
server.xml檔案,找到Connector標籤,加入URIEncoding="UTF-8"即可。
Ex:
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
connectionTimeout="30000"
redirectPort="8443" />
<Connector port="8080" protocol="HTTP/1.1"
URIEncoding="UTF-8"
connectionTimeout="30000"
redirectPort="8443" />
快速鍵 | 效果 |
CTRL + G | 移動到指定行號位置 |
CTRL + F4 | 關閉程式頁籤 |
CTRL + F | 尋找 |
CTRL + SHIFT + F | 在檔案中尋找 |
CTRL + H | 取代 |
CTRL + SHIFT + H | 在檔案中取代 |
CTRL + ALT + L | 檢視方案總管 |
CTRL + ALT + X | 檢視工具列 |
F5 | 開始偵錯 |
CTRL + F5 | 啟動但不偵錯 |
CTRL + SHIFT + W | 在瀏覽器中顯示 |
<?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/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Website</display-name> <servlet> <servlet-name>Website</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Website</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> </welcome-file-list> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> </web-app>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy /> <context:component-scan base-package="website" /> <!-- viewResolver setup --> <context:component-scan base-package="website.controller" /> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
@RequestMapping("/test.do") public ModelAndView test(HttpServletRequest req, HttpServletResponse resp) throws Exception { return new ModelAndView("demo"); }
public String arrayToString(String[] input){ StringBuffer result = new StringBuffer(); if(input!=null){ boolean isFirstElement = true; for(int i = 0; i<input.length; i++){ if(isFirstElement){ result.append(input[i]); } else { result.append(", "); result.append(input[i]); } } } return result.toString(); }
public String arrayToString(String[] input){ StringBuffer result = new StringBuffer(); if(input!=null && input.length>0){ result.append(input[0]); for(int i = 1; i<input.length; i++){ result.append(", "); result.append(input[i]); } } return result.toString(); }