home 控制器
package ghy.webapp.myapp;import java.text.dateformat;import java.util.date;import java.util.locale;import org.slf4j.logger;import org.slf4j.loggerfactory;import org.springframework.stereotype.controller;import org.springframework.ui.model;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestmethod;import org.springframework.web.bind.annotation.requestparam;/** * handles requests for the application home page. */@controllerpublic class homecontroller {private static final logger logger = loggerfactory.getlogger(homecontroller.class);/** * simply selects the home view to render by returning its name. */@requestmapping(value = "/", method = requestmethod.get)public string home(locale locale, model model) {logger.info("welcome home! the client locale is {}.", locale);date date = new date();dateformat dateformat = dateformat.getdatetimeinstance(dateformat.long, dateformat.long, locale);string formatteddate = dateformat.format(date);model.addattribute("servertime", formatteddate );return "home";}@requestmapping(value = "/two", method = requestmethod.get)public string two(int id,model model) {logger.info("two");system.out.println("hello2 action:"+id); model.addattribute("name", "huangjie"); //这个只有值没有键的情况下,使用object的类型作为key,string-->string model.addattribute("ok"); return "two";}}
登录后复制 two.jsp 视图
欢迎 hello world!
登录后复制 当前ip: two控制器
package ghy.webapp.myapp;import org.springframework.stereotype.controller;import org.springframework.web.bind.annotation.requestmapping;import org.springframework.web.bind.annotation.requestparam;import org.springframework.web.bind.annotation.responsebody;@controller public class twocontroller { @requestmapping("login") //用来处理前台的login请求 private @responsebody string hello( @requestparam(value = "username", required = false)string username, @requestparam(value = "password", required = false)string password ){ return "hello "+username+",your password is: "+password; } }
登录后复制 环境:sts +tomcat 运行 http://localhost:8080/myapp 执行home控制器 ,http://localhost:8080/myapp/two?id=3525是调用 two控制器传参id
5.下载地址:。 下载
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/3110987.html