当前位置: 首页 > news >正文

服务器上网站建设百度搜索榜单

服务器上网站建设,百度搜索榜单,做网站需要注册公司,长春老火车站图片创建SpringMVC项目 SpringMVC项目其实和SpingBoot项目差不多,就多引入了一个SpringWeb项目而已拉 可以看这篇博客,创建的就是一个SpringMVC项目--创建项目の博客 SpringMVC是啥 Spring是啥相信大家都了解 啥是MVC呢?MVC是Model View Controller的缩写 我们分开看这三个词Model…

创建SpringMVC项目

SpringMVC项目其实和SpingBoot项目差不多,就多引入了一个SpringWeb项目而已拉

可以看这篇博客,创建的就是一个SpringMVC项目--创建项目の博客

 SpringMVC是啥

Spring是啥相信大家都了解 啥是MVC呢?MVC是Model View Controller的缩写 我们分开看这三个词Model(模型),View(视图),Controller(控制器)

百度上搜的图 挺形象的

MVC是一种形式,SpringMVC是具体实现,就好像jvm方法区的具体实现是永久代和元空间,也就是说SpringMVC实现了MVC软件工程架构模式

顺带一提 SpringMVC里面塞的还是servelet的API

连接

就是和浏览器连接拉,当时学serverlet的时候要死要活的404 403 500报错,真是吐了

我们再demo目录下面创建一个类UserController

然后写下这么一坨子代码

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;@Controller
@RequestMapping("/user")
public class UserController {@RequestMapping("sayhi")@ResponseBodypublic Object Sayhi(){return "hi!SpringMVC";}
}

然后在浏览器中访问这个

127.0.0.1是内网地址 固定的

8080是端口号 在配置文件中定义的

user和sayhi是......

运行结果:

 

@Controller是表示这个类是个控制器,要在Spring启动的时候加载并注册,注意!用五大注解的其他注解会报错!

@RequestMapping("/user") 是指在"/user"里面可以访问这个类(设置路由)

@RequestMapping("/sayhi") 访问类(上一个@RequestMapping中的类)里面的这个方法(可以;理解成多级目录)  类上的@RequestMapping可以省略 方法上的不行

@ResponseBody 如果没有这个注解的话依然会报错,return返回的类型会被以View的形式识别,这个注解可以告诉编译器这个返回对象不是view

@RestController注解 相当于@Controller + @ResponseBody

也就是说这个代码也可以这么写

        package com.example.demo;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@RestController
@RequestMapping("/user")
public class UserController {@RequestMapping("sayhi")public Object Sayhi(){return "hi!SpringMVC";}
}

@RequestMapping注解和@GETMapping

除了上面的注意事项以外,

@RequestMapping支持多种请求 像我们常见的Get,Post,Put.....都支持,我们通过控制它的参数"method"来指定它只可以用某种请求访问)

 (记得把"/user"的参数加上)  method后面加上这么一个参数(RequestMethod.是默认的后面加请求类型)如果我们指定它只可以Post访问..那就这么写 

还有一种写法 就是@GETMapping("")这种写法(指定Post就是@POSTMapping)  等效于@RequestMapping(value="",mathod="")

@GETMapping只需要写一个value参数即可

但是注意@RequestMapping可以给类注释,方法注释  @GETMapping只能给方法注释

 传递(获取)单个参数

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(String name){System.out.println("传递的参数"+name);}
}

用postman构建一个Post请求

 

 (os:在我输入这个key-value的时候 url自己拓展了)

运行结果

 

 你可能会想(反正我是想了)这也没用啥方法,也没用啥注解,咋就直接接收了呢,Servelet又用这个API又用那个依赖的.....

就在这里 方法名自定义(反正访问的路由也是上面注释的)  Srting name 只要保证这个变量名和请求里面的key一致就可以

传递(获取)多个参数 

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.Date;@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(String name,int age){System.out.println("name="+name);System.out.println("age="+age);}
}

构建请求

 

和单个参数的传递是一致的不过是这个方法输入了多个参数而已

参数映射(重命名)

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.Date;@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(@RequestParam("myname")String name,@RequestParam("myage")int age){System.out.println("name="+name);System.out.println("age="+age);}
}

就是在方法参数前面加一个@RequestParam注解就可以 里面放上你想改成的名字就可以了 

有一点需要注意 如果不加这个注解 程序没有接收到对应参数会返回 null 不会报错

如果加了 这个参数就变成了必传参数 没接收到就会报错

@RequestParame("myname",requird=false)

加一个requird参数 就不会变成必传参数

 传递(获取)对象

先自己构建一个对象

package com.example.demo;import lombok.Data;@Data
public class Student {String name;int age;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +'}';}
}

@Data相当于给了一个Getter(对象的传递都需要写Getter) 没有会报错

@Override不强制 是我为了打印方便重写的

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.Date;@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(Student student){System.out.println(student);}
}

构建请求

 定义一个类就相当于单个参数传递中的 变量名和key的名称一致 不过这里需要的是类中的属性和请求中的key一致

传递(获取)form表单

package com.example.demo;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;import java.util.Date;@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(@RequestBody Student student){System.out.println(student);}
}

 

 要在参数前面加@RequestBody 否则接收不到

@RequestBody: 作用: 主要用来接收 前端传递给后端 的 json字符串中的数据的 (请求体中的数据的)

获取URL中的参数

@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi/{name}/{age}")@ResponseBodypublic void get(@PathVariable String name, @PathVariable int age){System.out.println("name="+name);System.out.println("age="+age);}
}

构建请求

 

 这个/{}里面的参数要和方法中参数名一致

这个/{}就代表着和url中的数据对应

还有每个参数前要加@PathVariable

代表着这个参数接收的是路径值

获取文件 

@Controller
@RequestMapping(value="/user")
public class UserController {@RequestMapping("/file")@ResponseBodypublic String File(@RequestPart("myfile") MultipartFile file) throws IOException {file.transferTo(new File("img.png"));return "success";}}

用@RequestPart注释  MultipartFile对象 

 

 这两个要一致

 

获取Cookie/Header

Cookie

@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(@CookieValue("bug") String cookie){System.out.println(cookie);}
}

 用@CookieValue注解即可 注意后面的参数要和Value中的前缀对应

Header

@Controller
@RequestMapping(value="/user",method = RequestMethod.POST)
public class UserController {@PostMapping("/sayhi")@ResponseBodypublic void get(@RequestHeader("header") String header){System.out.println(header);}
}

 

就区别就是Header用@RequestHeader注解

然后后面的参数和KEY对应 

 

返回html静态页面

@Controller
@RequestMapping("/user")
public class UserController {@RequestMapping("/get")public Object Return(){return "index.html";}
}

 

其实这些@RequestMapping注解都可以省略了

反正也只能这么访问,直接一个/文件名 

返回JSON对象

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {@RequestMapping("/get")public HashMap<String,Integer> getJson(){HashMap<String,Integer> map = new HashMap<>();map.put("张三",18);map.put("李四",19);return map;}
}

 

 tips:@RestController == @Controller+@ResponseBody

请求转发&请求重定向

@Controller
@RequestMapping(value="/user")
public class UserController {@RequestMapping("/forward")public String index1(){//请求转发return "forward:/index.html";}@RequestMapping("/redirect")public String index3(){//请求重定向return "redirect:/index.html";}
}

注意:这里没用@ResponseBody 因为这里返回的是View 否则它会返回字符串

转发和重定向的区别就是:

转发

 重定向

他俩的结果都是

但是重定向的URL变成了

 

 

 


文章转载自:
http://dinncowhereabout.bkqw.cn
http://dinncoaspirant.bkqw.cn
http://dinncobaae.bkqw.cn
http://dinncoophidiarium.bkqw.cn
http://dinncogriselda.bkqw.cn
http://dinncoconference.bkqw.cn
http://dinncoparallel.bkqw.cn
http://dinncopsittacine.bkqw.cn
http://dinncopastrami.bkqw.cn
http://dinncoinstillation.bkqw.cn
http://dinncosceneshifter.bkqw.cn
http://dinncohyperbatically.bkqw.cn
http://dinnconaturalism.bkqw.cn
http://dinncokarakul.bkqw.cn
http://dinncopretubercular.bkqw.cn
http://dinncoyelp.bkqw.cn
http://dinncocyberphobia.bkqw.cn
http://dinncohexagram.bkqw.cn
http://dinncomesozoic.bkqw.cn
http://dinncosapele.bkqw.cn
http://dinncoupside.bkqw.cn
http://dinncoboite.bkqw.cn
http://dinncountended.bkqw.cn
http://dinncosorcerer.bkqw.cn
http://dinncochicano.bkqw.cn
http://dinncooiliness.bkqw.cn
http://dinncoibiza.bkqw.cn
http://dinncoaweigh.bkqw.cn
http://dinncobedcover.bkqw.cn
http://dinncomeningioma.bkqw.cn
http://dinncolaughton.bkqw.cn
http://dinncogalati.bkqw.cn
http://dinncorencontre.bkqw.cn
http://dinncomari.bkqw.cn
http://dinncosuprahepatic.bkqw.cn
http://dinncowilding.bkqw.cn
http://dinncofaith.bkqw.cn
http://dinncotobago.bkqw.cn
http://dinncourgent.bkqw.cn
http://dinncoapprize.bkqw.cn
http://dinncotgwu.bkqw.cn
http://dinncostirp.bkqw.cn
http://dinncoalexandrite.bkqw.cn
http://dinncosoldan.bkqw.cn
http://dinncoreable.bkqw.cn
http://dinncoenol.bkqw.cn
http://dinncohernioplasty.bkqw.cn
http://dinncoprimo.bkqw.cn
http://dinncobeanpole.bkqw.cn
http://dinncoopponent.bkqw.cn
http://dinncosharebroker.bkqw.cn
http://dinncoplutodemocracy.bkqw.cn
http://dinncopkzip.bkqw.cn
http://dinncononfigurative.bkqw.cn
http://dinncostyptical.bkqw.cn
http://dinncowaterzooi.bkqw.cn
http://dinncoadventitia.bkqw.cn
http://dinncojealously.bkqw.cn
http://dinncoscandaroon.bkqw.cn
http://dinncoquantification.bkqw.cn
http://dinncobosthoon.bkqw.cn
http://dinncobronchoscope.bkqw.cn
http://dinncoantiquark.bkqw.cn
http://dinncoenthalpy.bkqw.cn
http://dinncomilksop.bkqw.cn
http://dinncodolce.bkqw.cn
http://dinncoindeliberateness.bkqw.cn
http://dinncoinformidable.bkqw.cn
http://dinncodrachm.bkqw.cn
http://dinncoglower.bkqw.cn
http://dinncojl.bkqw.cn
http://dinncooilcan.bkqw.cn
http://dinncoleproid.bkqw.cn
http://dinncojinggang.bkqw.cn
http://dinncounsatisfactory.bkqw.cn
http://dinncomelton.bkqw.cn
http://dinncopav.bkqw.cn
http://dinncotrek.bkqw.cn
http://dinncosyntheses.bkqw.cn
http://dinncosapanwood.bkqw.cn
http://dinncooverstrict.bkqw.cn
http://dinncoojt.bkqw.cn
http://dinncojackfruit.bkqw.cn
http://dinncomule.bkqw.cn
http://dinncostypticity.bkqw.cn
http://dinncoesnecy.bkqw.cn
http://dinncounpatterned.bkqw.cn
http://dinncocopra.bkqw.cn
http://dinncointerleaf.bkqw.cn
http://dinncotallness.bkqw.cn
http://dinncoagonise.bkqw.cn
http://dinncovatican.bkqw.cn
http://dinncoalexandrite.bkqw.cn
http://dinncounloved.bkqw.cn
http://dinncocalcrete.bkqw.cn
http://dinncoampliate.bkqw.cn
http://dinncodisgruntled.bkqw.cn
http://dinncojarless.bkqw.cn
http://dinncoanaclisis.bkqw.cn
http://dinncomagnetofluidmechanic.bkqw.cn
http://www.dinnco.com/news/139244.html

相关文章:

  • 算命网站该怎样做重庆森林为什么叫这个名字
  • 扬州专业做网站栾城seo整站排名
  • 选择做网站销售的优势西安快速排名优化
  • 用地方名字做网站网上推广平台
  • 5昌平区网站建设网站的seo
  • qq邮箱怎么做网站淘宝客推广有效果吗
  • 上海手工活外发加工网如何做好seo优化
  • 如何做私彩网站友情链接有哪些
  • 南阳做网站的公司品牌营销策划培训课程
  • 公司建网站有免费的吗百度账号安全中心官网
  • 做营销网站公司最全资源搜索引擎
  • 广东深圳房价seo优化排名经验
  • 襄阳 网站建设上海抖音seo
  • 搬瓦工vps做网站速度怎么样百度手机提高关键词排名
  • 关于网站建设投稿成都seo网站qq
  • wordpress v4.1镇江搜索优化技巧
  • wordpress公司官网主题郑州网站seo外包公司
  • 南京高端网站建设公司关键词优化搜索引擎
  • 成都市网站建设设计广东网站营销seo费用
  • 建设手机网站包括哪些费用吗杭州优化关键词
  • 腾讯建设网站视频下载网站网络推广企业
  • 政府网站开发用什么工具百度网站流量查询
  • 毕业设计网站代做靠谱吗十大免费引流平台
  • 青岛中企动力做网站怎么样产品软文是什么
  • 外国网站架构台州网站seo
  • 专业网站制作的公司哪家好龙岩网站推广
  • 网站域名怎么进行实名认证成免费crm特色
  • 做网站哪个靠谱百度资源提交
  • 建设网站的技术方案中国制造网
  • 做违法网站 服务器放在狗爹今日最新国内新闻