会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 134125个问题

from flask import Flask,url_for
from flask_restful import Api,Resource,reqparse

app = Flask(__name__)
api = Api(app)


@app.route('/')
def hello_world():
    return 'Hello World!'


# Flask_RESTful的基本使用
# 定义一个类视图
class LoginView(Resource):
    def get(self):
        return {'flag':'no'}
    def post(self):
        return {'flag':'yes'}

# 映射url
# api.add_resource(LoginView,'/login/','/login2/')
api.add_resource(LoginView,'/login/','/login2/',endpoint='login')

with app.test_request_context():
    # print(url_for('loginview'))     # 如果不写endpoint,那么将会使用视图函数的名字的小写作为endpoint。
    print(url_for('login'))     # 如果指定endpoint,就用视endpoint的值来指定url。


# Flask_RESTful功能之参数验证    基本用法
class RegisterView(Resource):
    def post(self):
        # 用户名 uname
        # 1.创建一个解析器对象
        parse = reqparse.RequestParser()
        # 2.利用解析器对象   添加需要验证的参数   并指定验证规则
        parse.add_argument('uname',type=str,help='用户名不符合规则!!!',required=True,trim=True)
        # 3.利用解析器对象   进行验证    若正确,直接返回验证后合格的参数值,反之,抛异常信息给客户端
        args = parse.parse_args()
        # 若验证成功后,需要插入数据库
        print(args)
        return {'tips':'注册成功'}

api.add_resource(RegisterView,'/register/')


if __name__ == '__main__':
    app.run(debug=True)

请问,这里的请求方式为什么变成了“get”?


Python全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask高级 30512楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 30513楼
JAVA 全系列/第二十二阶段:租房网(Spring Cloud最新架构)/Livegoods第一天 30515楼
Python全系列/第十六阶段:Python 爬虫开发/爬虫反反爬- 30516楼
Python全系列/第三阶段:Python 网络与并发编程/并发编程 30517楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 30520楼
JAVA 全系列/第八阶段:Linux入门到实战/Linux(旧) 30521楼

老师我跟着视频又写了一遍,同样的代码在demo(我用来跟着老师敲的工程)可以成功跑起来,监听器也会被触发那些Added     Removed    Replaced    的输出语句再清空控制台然后访问    attr.do    也可以被打印出来,但是在demo_test(我用来练习的工程)就没法触发监听器,导致清空控制台再访问    attr.do只有以下结果


image.png


但是如果监听器没被触发那么为什么启动Tomcat控制台就会有以下结果


image.png


源码如下:

package com.bjsxt.listener;

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;

/**
 * ServletContext对象属性操作监听器
 */
public class ServletContextAttrListener implements ServletContextAttributeListener {
    @Override
    public void attributeAdded(ServletContextAttributeEvent scae) {
        System.out.println("---------------Added Started-----------------");
        //获取属性名和属性值
        System.out.println(scae.getName()+"-------"+scae.getValue());
        //取出ServletContext对象
        System.out.println(scae.getServletContext());
        System.out.println("---------------Added Ended-----------------");
    }

    @Override
    public void attributeRemoved(ServletContextAttributeEvent scae) {
        System.out.println("---------------Removed Started-----------------");
        //获取属性名和属性值
        System.out.println(scae.getName()+"-------"+scae.getValue());
        //取出ServletContext对象
        System.out.println(scae.getServletContext());
        System.out.println("---------------Removed Ended-----------------");
    }

    @Override
    public void attributeReplaced(ServletContextAttributeEvent scae) {
        System.out.println("---------------Replaced Started-----------------");
        //获取属性名和属性值
        System.out.println(scae.getName()+"-------"+scae.getValue());
        //取出ServletContext对象
        System.out.println(scae.getServletContext());
        System.out.println("---------------Replaced Ended-----------------");
    }
}
package com.bjsxt.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/attr.do")
public class ServletContextAttrServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //调用测试监听器的三个方法
        servletContext.setAttribute("key","BJSXT");
        servletContext.setAttribute("key","ITBZ");
        servletContext.removeAttribute("key");
    }
}









JAVA 全系列/第六阶段:JavaWeb开发/Servlet技术详解(旧) 30523楼

麻烦老师帮我看下为什么报错。

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException: 

### Error querying database.  Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver 

### The error may exist in com/bjsxt/pojo/mapper/employee2mapper.xml

### The error may involve com.bjsxt.pojo.mapper.employee2mapper.selectall

### The error occurred while executing a query

### Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver 

at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:149)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:140)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:135)

at com.bjsxt.pojo.dao.impl.employee2impl.selectall(employee2impl.java:22)

at com.bjsxt.pojo.test.Test.main(Test.java:12)

Caused by: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: com.mysql.jdbc.Driver 

at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.initializeDriver(UnpooledDataSource.java:244)

at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:223)

at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.doGetConnection(UnpooledDataSource.java:219)

at org.apache.ibatis.datasource.unpooled.UnpooledDataSource.getConnection(UnpooledDataSource.java:95)

at org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(PooledDataSource.java:432)

at org.apache.ibatis.datasource.pooled.PooledDataSource.getConnection(PooledDataSource.java:89)

at org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(JdbcTransaction.java:139)

at org.apache.ibatis.transaction.jdbc.JdbcTransaction.getConnection(JdbcTransaction.java:61)

at org.apache.ibatis.executor.BaseExecutor.getConnection(BaseExecutor.java:337)

at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:86)

at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62)

at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:325)

at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)

at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)

at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:89)

at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:147)

... 4 more


Process finished with exit code 1

mybatisdemo.zip


JAVA 全系列/第六阶段:项目管理与SSM框架/Mybatis 30525楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637