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参数呢?
继续改造代码。
@RequestParam(value = "string",required = false)String password
这句话的意思就是我传入一个get变量为string的参数,分配到java里面就是变量password,字符型的。
required = false可加可不加。加的好处就是如果用户不传这个string过来。页面不会报错,而不加就会报错。
这个报的错是系统给的,我们控制不了。
GET解决了。现在我要POST呢?
现在做一下改造
hello.jsp变成
这样页面就变成了我要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呢?我两个都要接受,没问题,我再接着改造。
其它代码略过,主要就是这几句。
然后我再访问页面。
大功告成!
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) {
其它代码略过,主要就是这几句。
然后我再访问页面。
大功告成!
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- GUI - Web前端开发框架
- 介绍一款信息管理系统的开源框架---jeecg
- Extjs4.0 最新最全视频教程
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序