您的位置:首页 > 编程语言 > Java开发

springmvc返回值、数据写到页面、表单提交、ajax、重定向

2016-01-30 11:52 609 查看
实验是在前一篇文章的项目上做的;

数据写到页面

后台往前台传数据

TestController添加

/**
* 方法的返回值采用ModelAndView, new ModelAndView("index", map);,
* 相当于把结果数据放到request里面
* @return
* @throws Exception
*/
@RequestMapping("/toPerson4.do")
public ModelAndView toPerson4() throws Exception{
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date);

Map<String,Object> map = new HashMap<String, Object>();
map.put("p", person);
return new ModelAndView("jsp/index",map);
}


  页面接收:index.jsp

<body>
<h5>${p.name }</h5>
<h5>${p.age }</h5>
<h5>${p.address }</h5>
<h5><fmt:formatDate value="${p.birthday }" pattern="yyyy-MM-dd"/></h5>
</body>


  

在jsp引入fmt标签库
* 文章包含被禁用的url,无法保存和发布。

太坑了,这个链接也屏蔽~
重启tomcat访问:
http://localhost:8080/springmvc-2/test/toPerson4.do 输出信息正确;

另外一种方式:

/**
* 直接在方法的参数列表中来定义Map,这个Map即使ModelAndView里面的Map
* 由视图解析器统一处理,统一走ModelAndView的接口
* 也不建议使用
* @param map
* @return
* @throws Exception
*/
@RequestMapping("/toPerson5.do")
public String toPerson5(Map<String,Object> map) throws Exception{
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date);

map.put("p", person);
return "jsp/index";
}


  

重启tomcat访问: http://localhost:8080/springmvc-2/test/toPerson5.do 输出正确;

建议使用方式:

/**
*在参数列表中直接定义Model,model.addAttribute("p", person);
*把参数值放到request类里面去,建议使用
* @param map
* @return
* @throws Exception
*/
@RequestMapping("/toPerson6.do")
public String toPerson6(Model model) throws Exception {
Person person = new Person();
person.setName("jerome");
person.setAge(22);
person.setAddress("nanan");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-12-21");
person.setBirthday(date);
//把参数值放到request类里面去
model.addAttribute("p", person);
return "jsp/index";
}


  

重启tomcat访问: http://localhost:8080/springmvc-2/test/toPerson6.do 输出正确数据;

不需要页面跳转:ajax

后台方法:

在TestController加

/**
* ajax的请求返回值类型应该是void,参数列表里直接定义HttpServletResponse,
* 获得ProntWriter的类,最后可把结果写到页面
* 不建议使用
* @param name
* @param response
*/
@RequestMapping("/ajax.do")
public void ajax(String name, HttpServletResponse response) {
String result = "hello " + name;
try {
response.getWriter().write(result);
} catch (IOException e) {
e.printStackTrace();
}
}


  前台调用 新建ajax.jsp
<input id="myButton" type="button" value="click">
Webroot新建js文件夹将jquery拷贝进来;
引进来jquery 和写js脚本:

<script type="text/javascript">
$(function(){
$("#myButton").click(function(){
$.ajax({
url:"test/ajax.do",
type:"post",
dataType:"text",
data:{
name:"zhangsan"
},
success:function(responseText){
alert(responseText);
},
error:function(){
alert("system error");
}
});
});
});
</script>


  后台写一个转发:

@RequestMapping("/toAjax.do")
public String toAjax() {
return "jsp/ajax";
}


  

重启tomcat 访问 http://localhost:8080/springmvc-2/test/toAjax.do Click,弹出hello zhangsan,成功;

以上方法不建议使用,建议使用:

/**
* 直接在参数的列表上定义PrintWriter,out.wrote(result);
* 把结果写到页面,建议使用
* @param name
* @param out
*/
@RequestMapping("/ajax1.do")
public void ajax1(String name, PrintWriter out) {
String result="hello1 "+name;
out.write(result);
}


  

修改ajax.jap页面的,js脚本,跳转的url是
url:"test/ajax1.do",
重启tomcat 访问 http://localhost:8080/springmvc-2/test/toAjax.do Click
弹出 hello1 zhangsan;

表单:

拷贝一份index,起名form.jsp

<body>
<form action="test/toPerson7.do" method="post">
name:<input name="name" type="text"><br/>
age:<input name="age" type="text"><br/>
address:<input name="address" type="text"><br/>
birthday:<input name="birthday" type="text"><br/>
<input type="submit"><br/>
</form>


  TestController

@RequestMapping("/toPerson7.do")
public String toPerson7(Person person) {
System.out.println(person);
return "jsp/index";
}


  重启tomcat 访问: http://localhost:8080/springmvc-2/test/toForm.do 提交跳转到 http://localhost:8080/springmvc-2/test/toPerson7.do 控制台输出
Person [name=aa, address=asdf, birthday=Tue Jun 03 00:00:00 CST 2014, age=22]

请求方式的指定:

后台可以指定提交方法,如果前台不是用的同一种提交方式 将报错;

/**
* @RequestMapping(value="/toPerson7.do",method=RequestMethod.POST)
* 可以指定请求方式,前台就必须要以它制定好的方式来访问,不然会出现405错误
* @param person
* @return
*/
@RequestMapping(value="/toPerson7.do",method=RequestMethod.POST)
public String toPerson7(Person person) {
System.out.println(person);
return "jsp/index";
}


  

Form.jap的method修改为get和post测试;

重定向:

同一个 controller

/**
* 重定向:controller内部重定向,redirect:加上同一个controller中
* 的requesMapping的值
* @return
*/
@RequestMapping("/redirectToForm.do")
public String redirectToForm() {
return "redirect:toForm.do";
}


  

重启tomcat 访问: http://localhost:8080/springmvc-2/test/redirectToForm.do 重定向到 http://localhost:8080/springmvc-2/test/toForm.do

Controller之间的重定向:

拷贝一份TestController改成TestController1
留这个

@Controller
//用来标注当前类是springmvc的控制层的类
@RequestMapping("/test1")
//controller的唯一标识或者命名空间
public class TestController1 {

@RequestMapping("/toForm.do")
public String toForm() {
return "jsp/form";
}

}


  TestController 添加

/**
*  controller之间的重定向:必须要指定好controller的命名空间再
*  指定requestMapping的值,redirect:后必须要加/,是从根目录开始,
*  否则就从当天test找了
* @return
*/
@RequestMapping("/redirectToForm1.do")
public String redirectToForm1() {
return "redirect:/test1/toForm.do";
}


  重启tomcat 访问
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: