个人知乎
基础二-WEB知识
SpringBoot工程:类似Spring,但使用注解而不是配置文件
框架学习:看官方文档样例,读个大概,具体问题具体查
start.spring.io:
自动配置好一个spring项目,下载解压即可
pom.xml:
IDEA通过打开pom文件导入到文件
目录结构
java:源代码目录
java.controller:入口控制
java.model:数据模型,与数据表匹配
java.dao:数据库接口,与数据表匹配
java.service:服务接口
resources:资源
resources.static:静态文件
resources.templates:模板
最简单的服务器:
web->Controller->Service->DAO->Database
Controller注解:
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/" ,"/index" })
@ResponseBody
public String index (){
return "hello,world" ;
}
}
参数解析
path到参数:
@Controller
public class ControllerOfindex {
@RequestMapping (/profile/{userId})
@ResponseBody
public String profile (@PathVariable ("userId") int userId
@RequestParm (value="type" ,defaultValue="100" ,requird=false ) int type){
return String.format("page of %d" ,userId);
}
}
HTTP Method
Postman
配合360浏览器查看网页
GET/POST:表单只支持这两种方式
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/" ,"/index" },method={RequestMethdo.GET})
@ResponseBody
public String index (){
return "hello,world" ;
}
}
PUT
支持幂等性的POST,只执行一次
Velocity
静态和模板
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/vm" ,},method={RequestMethdo.GET})
public String template (){
return "home" ;
}
}
数据到模板
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/vm" ,},method={RequestMethdo.GET})
public String template (Model model){
model.addAttribute("value1" ,"vvvvv" );
model.addAttribute("colors" ,Arrays.asList(new String[]={"red" ,
"blue" ,"yellow" }));
return "home" ;
}
}
模板语言:velocity基础
<html >
##模板语言注释
## $:变量标志
## {}:java传入的变量
## #:控制语句
## $变量能够直接调用java方法
$!{value1}##!:如果不存在强制为空
${value2}#*不存在则显示字面值{value2}*#
colors:$!{colors} ##列表数据
#foreach($color in $colors)
This is Color $!{foreach.index}:$color, $!(foreach.count)
#*foreach.index/foreach.count:模板自带的计数
*#
#end
User:$!{user.name}##会自动调用get方法
#set($title="nowcoder")##定义变量
Title:$!{title}
#parse("other.html") ##解析其他模板,公共部分
#include("other1.html") ##只是文本的包含,不解析
##函数定义
#macro (render_color,$index,$color)
Color $index,$color
#end
#*函数调用
*#
#foreach($color in $colors)
render_color($foreach.index,$color)
#end
## "":做解析的串连接
## '':直接内容的连接
set($hello="!${title}hello")
set($hello='!${title}hello')
</html >
Request/Response/Session
Request:参数解析,cookie读取,http请求,二进制文件内容
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/request" ,},method={RequestMethdo.GET})
@RequestBody
public String template (Model model,HttpServletResponse response,
HttpServletRequest request,HttpSession httpsession){
request.getMethod();
request.getQueryString();
request.getPathInfo();
request.getRequestURI();
Enumeration<String> headnames=request.getHeaderNames();
while (headnames.hasNext()){
String a=headnames.nextElement();
}
request.getCookies();
response.addHeader("" ,"" );
response.addCookie(new Cookie("" ,"" ));
response.getOutputStream().write();
}
}
Error/重定向
301
永久性
302
临时性
@Controller
public class ControllerOfindex {
@RequestMapping (path={"/redirect/{code}" },method={RequestMethdo.GET})
public String redirect (@PathVariable ("code") int code){
return "redirect:/" ;
}
@RequestMapping (path={"/redirect/{code}" },method={RequestMethdo.GET})
public RedirectView redirect (@PathVariable ("code") int code){
RedirectView rd =new RedirectView("/" ,true );
if (code==301 ){
rd.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
}
return rd
}
@ExceptionHandler ()
@ResponseBody
public String error (Exception e){
return "error:" +e.getMessage();
}
}
Logger
根据类全名构造一个该类的Logger,帮助记录各种信息
IoC
按照配置文件自动的构造对象实例后依赖注入到使用的地方,
而不用手动new
@Service
class WendaService{
public String doSomething (){
return "hi,world" ;
}
}
@Controller
public class ControllerOfindex {
@Autowired
WendaServiec wenda;
@RequestMapping (path={"/" ,"/index" },method={RequestMethdo.GET})
@ResponseBody
public String index (){
return "hello,world" +wenda.doSometing();
}
}
AOP/Aspect
对所有服务(一系列步骤)时,插入一个共同的步骤,关注一个切面
@Aspect
@Component
public class LogAspect {
@Before ("execution(* com.nowcoder.controller.IndexController.*(..))" )
public void beforeMethod (JoinPoint joinPoint){
joinPoint.getArgs();
}
@After ("execution()" )
public void aferMethod (){
}
}