当前位置:Gxlcms > html代码 > srpingmvc学习_html/css_WEB-ITnose

srpingmvc学习_html/css_WEB-ITnose

时间:2021-07-01 10:21:17 帮助过:16人阅读

  1. HOME 控制器
    1. 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 {
    2. private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
    3. /**
    4. * Simply selects the home view to render by returning its name.
    5. */
    6. @RequestMapping(value = "/", method = RequestMethod.GET)
    7. public String home(Locale locale, Model model) {
    8. logger.info("Welcome home! The client locale is {}.", locale);
    9. Date date = new Date();
    10. DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    11. String formattedDate = dateFormat.format(date);
    12. model.addAttribute("serverTime", formattedDate );
    13. return "home";
    14. }
    15. @RequestMapping(value = "/Two", method = RequestMethod.GET)
    16. public String Two(
    17. int id,Model model
    18. ) {
    19. logger.info("two");
    20. System.out.println("hello2 action:"+id); model.addAttribute("name", "huangjie"); //这个只有值没有键的情况下,使用Object的类型作为key,String-->string model.addAttribute("ok");
    21. return "Two";
    22. }
    23. }
  2. Two.jsp 视图
    1. <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>欢迎</title> <h2>Hello World!</h2> <span>当前IP:<%=request.getRemoteAddr() %></span>
  3. Two控制器
    1. 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
  4. 运行 http://localhost:8080/myapp 执行home控制器 ,http://localhost:8080/myapp/Two?id=3525是调用 Two控制器传参id

5.下载地址:。 下载

人气教程排行