ajax,springmvc,json新手使用
老是使用jsp传数据感觉没有跟上时代。所有就课外学习了一下ajax,和json传值。并且用在springmvc框架中
前台页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title></title> <script src="js/jquery-1.9.1.min.js"></script> <script language="Javascript"> function checkUserid() { $.ajax({ type : 'post', data : { mainScore : $("#userid").val(), }, url : "test.do", success : function(data) { alert(data); }, error : function() { }, complete : function() { } }); } </script> </head> <body> 用户ID: <input type="text" id="userid" name="userid"> <span id="msg"></span> <br> <button onclick="checkUserid()">传输</button> <p></p> </body> </html>
web.xml
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
controller设置
@Controller public class test { @RequestMapping("test") public @ResponseBody List<String> test(String mainScore){ List<String> data=new ArrayList<String>(); data.add(mainScore); return data; } }
springmvc的配置文件没发出来,我相信你们应该没问题。如有问题,请百度
没有虚过一天,真好