spring的第四天

全弄好之后,要看一下系统整出来的代码了。这应该是最基本的DEMO,非常具有帮助性。

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/")
public class HelloController {
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		model.addAttribute("message", "Hello world!");
		return "hello";
	}
}

这里的return "hello"是加载静态的html页面,即MVC中的V层文件。如下图中的hello.jsp

<html>
<body>
	<h1>${message}</h1>
</body>
</html>

代码非常简单!只是为了入门嘛。
<h1>${message}</h1>接受的就是C层中的model.addAttribute("message", "Hello world!");

如果我要接受传出来的get参数呢?
继续改造代码。

package com.springapp.mvc;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/")
public class HelloController {
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model,@RequestParam(value = "string",required = false)String password) {

		model.addAttribute("message", password);
		return "hello";
	}
}

@RequestParam(value = "string",required = false)String password
这句话的意思就是我传入一个get变量为string的参数,分配到java里面就是变量password,字符型的。
required = false可加可不加。加的好处就是如果用户不传这个string过来。页面不会报错,而不加就会报错。
这个报的错是系统给的,我们控制不了。

GET解决了。现在我要POST呢?

现在做一下改造
hello.jsp变成

<%@ page contentType="text/html; charset=utf-8"%>
<html>
<body>
	<h1>${message}</h1>
	<form action="/web/getHello" method="post">
		UserName<input type="text" name="username">
		<br>
		PassWord<input type="password" name="password">
		<br>
		<input type="submit" value="提交">
	</form>
</body>
</html>

这样页面就变成了我要post两个参数:username和password给/web/getHello的页面。

注意,如果你在访问页面的时候发现中文是乱码。那估计就是漏了下面这句话。
<%@ page contentType="text/html; charset=utf-8"%>

然后在HelloController的同级目录下新建一个GetHelloController

@Controller
@RequestMapping("/getHello")
public class GetHelloController {
	@RequestMapping(method = RequestMethod.POST)
	public @ResponseBody void printWelcome(
			HttpServletResponse response,
			@RequestParam(value = "username",required = false)String username,
			@RequestParam(value = "password",required = false)String password) {
			response.setContentType("text/html; charset=gbk");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.println(username);
	}
}

这个代码简单,一目了然。实现了我接受post参数的需求,out.println是打印出来看。

那如果我即要get又要post呢?我两个都要接受,没问题,我再接着改造。

@RequestMapping(method ={RequestMethod.POST,RequestMethod.GET})
public @ResponseBody void printWelcome(
		HttpServletResponse response,
		@RequestParam(value = "username",required = false)String username,
		@RequestParam(value = "password",required = false)String password,
		@RequestParam(value = "type",required = false)String type) {

其它代码略过,主要就是这几句。
然后我再访问页面。

大功告成!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值