srping mvc学习_html/css_WEB-ITnose

WBOY
發布: 2016-06-24 11:27:29
原創
933 人瀏覽過

  1. 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";	}	}
    登入後複製
  2. Two.jsp 视图
    <%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  <title>欢迎</title>  </head>  <body>  <h2>Hello World!</h2>    <form action="login">      用户名:<input id="username" name="username" type="text" value='${name}'></input><br>      密  码:<input id="username" name="password" type="password"></input><br>      <input type="submit">  </form>  <span>当前IP:<%=request.getRemoteAddr() %></span>  </body>  </html>
    登入後複製
  3. 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
  4. 运行 http://localhost:8080/myapp 执行home控制器 ,http://localhost:8080/myapp/Two?id=3525是调用 Two控制器传参id

5.下载地址:。 下载

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板