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

spring的第四天

2016-07-07 14:08 513 查看
全弄好之后,要看一下系统整出来的代码了。这应该是最基本的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) {


其它代码略过,主要就是这几句。

然后我再访问页面。


大功告成!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息