个人知乎 ##基础二-WEB知识

个人知乎

基础二-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 //return内容直接到页面
    public String index(){
        return "hello,world";
    }    
}

参数解析

path到参数:
@Controller
public class ControllerOfindex{
    @RequestMapping(/profile/{userId})
    @ResponseBody //return内容直接到页面
    //用PathVariable+{}括号的方式解析url中内容
    //用RequestParam方式请求?后的参数,如/profile/1?type=1
    //RequestParam方式请求表单数据
    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{
    //GET获取数据,POST提交数据
    @RequestMapping(path={"/","/index"},method={RequestMethdo.GET})
    @ResponseBody //return内容直接到页面
    public String index(){
        return "hello,world";
    }    
}
PUT
支持幂等性的POST,只执行一次

Velocity

静态和模板
//先在template目录下创建home.html/home.vm
@Controller
public class ControllerOfindex{
    //GET获取数据,POST提交数据
    @RequestMapping(path={"/vm",},method={RequestMethdo.GET})
    public String template(){
        return "home";
    }    
}
数据到模板
@Controller
public class ControllerOfindex{
    //GET获取数据,POST提交数据
    @RequestMapping(path={"/vm",},method={RequestMethdo.GET})
    //model:与html交互的模型,传递给模板
    public String template(Model model){
        model.addAttribute("value1","vvvvv");
        model.addAttribute("colors",Arrays.asList(new String[]={"red",
                "blue","yellow"}));
        return "home";
    }    
}
模板语言:velocity基础
<!-- home.html -->
<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请求,二进制文件内容
Response:cookie下发,headers设置,页面内容
@Controller
public class ControllerOfindex{
    @RequestMapping(path={"/request",},method={RequestMethdo.GET})
    @RequestBody
    //形参有request response等变量,框架会自动传入
    public String template(Model model,HttpServletResponse response,
                HttpServletRequest request,HttpSession httpsession){
        //获取request方法,?参数等
        request.getMethod();
        request.getQueryString();
        request.getPathInfo();
        request.getRequestURI();
        //获取request头
        Enumeration<String> headnames=request.getHeaderNames();
        while(headnames.hasNext()){
            String a=headnames.nextElement();

        }
        //cookie
        request.getCookies();
        //返回的内容
        response.addHeader("","");
        response.addCookie(new Cookie("",""));
        response.getOutputStream().write();
    }    
}

Error/重定向

301
永久性
302
临时性
@Controller
public class ControllerOfindex{
    //GET获取数据,POST提交数据
    @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
    }  
    //统一处理抛出的Exception
    @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;
    //GET获取数据,POST提交数据
    @RequestMapping(path={"/","/index"},method={RequestMethdo.GET})
    @ResponseBody //return内容直接到页面
    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得到方法的参数等内容
        joinPoint.getArgs();

    }
    @After("execution()")
    public void aferMethod(){

    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值