自己動手實踐了一次,發生中間出了一下問題,現整理出來,供參考。 新建一個java web項目 下載spring的jar包 "http://repo.spring.io/libs release local/org/springframework/spring/" 版本取決於你,我選擇的是4.0.4的 ...
自己動手實踐了一次,發生中間出了一下問題,現整理出來,供參考。
Step1:
新建一個java web項目
Step2:
下載spring的jar包http://repo.spring.io/libs-release-local/org/springframework/spring/
版本取決於你,我選擇的是4.0.4的,下載好了之後進行解壓,將libs文件夾中jar文件都添加項目的lib中。
註意其中commons-logging-1.1.1.jar
包是需要另外下去添加進去的。
Step3:
配置spring功能。
首先在WEB-INF
文件夾下創建一個xml文件:spring-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
>
<!-- 解決中文亂碼 begin -->
<bean id="utf8Charset" class="java.nio.charset.Charset"
factory-method="forName">
<constructor-arg value="UTF-8"/>
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg ref="utf8Charset"/>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 解決中文亂碼 end -->
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 處理靜態資源begin -->
<mvc:default-servlet-handler />
<!-- 處理靜態資源end -->
<context:component-scan base-package="com.xsh.learnspring" />
</beans>
Step4:
配置web.xml
,在該文件中加入以下內容:
<!-- Spring Configures start -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- encoding filter for jsp page -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Spring Configures end -->
Step5:
創建jsp
和 controller
<body>
<input type = "button" value="test" onclick= "test()" id="test"/>
</body>
<script>
function test(){
var data = "請求";
$.ajax({
url: "test.do",
type:'post',
dataType: 'text',
data:{data:data},
success: function(datas){
console.log(datas);
}
});
}
</script>
/**
* ClassName: SpringTestController <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON(可選). <br/>
* date: 2016-6-18 上午10:40:38 <br/>
*
* @author xieshuang
* @version
* @since JDK 1.6
*/
@Controller
public class SpringTestController {
@RequestMapping ( "/test.do" )
@ResponseBody
public String myTest(HttpServletRequest request,
HttpServletResponse response) throws Exception{
String data = request.getParameter("data");
System.out.println("獲取的參數為:" + data);
String message = "訪問成功";
return message ;
}
}
1、前後臺交互傳遞數據時會出現中文亂碼,其配置必須放在<context:component-scan base-package="com.xsh.learnspring" />
上面
2、對項目靜態資源的處理,例如引入的jquery.js
<mvc:default-servlet-handler />