- 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 视图
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
当前ip:<%=request.getremoteaddr() %>欢迎 hello world!
- 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.下载地址:。 下载











