【问题标题】:CppCMS URL Mapping issueCppCMS URL 映射问题
【发布时间】:2017-08-11 22:25:52
【问题描述】:

我正在玩 CppCMS,我已经让静态的“Hello World”工作了。但是,我很难让 URL 映射正常工作。我确定我很傻,并且遗漏了一些明显的东西。

我遇到的问题是 URL 似乎不起作用。当我尝试访问 :8080/home/smile 时,我只会收到默认的“主”页面。

代码如下:

`#include <cppcms/application.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/applications_pool.h>
#include <iostream>
#include <stdlib.h>

class hello : public cppcms::application {
public:
    hello(cppcms::service &srv) :
            cppcms::application(srv)
    {
    dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
    mapper().assign("number","/number/{1}");

    dispatcher().assign("/smile",&hello::smile,this);
    mapper().assign("smile","/smile");

    dispatcher().assign("",&hello::welcome,this);
    mapper().assign("");

    mapper().root("/hello");
    }
        void number(std::string num)
    {
        int no = atoi(num.c_str());
        response().out() << "The number is " << no << "<br/>\n";
        response().out() << "<a href='" << url("/") << "'>Go back</a>";
    }
        void smile()
    {
        response().out() << ":-) <br/>\n";
        response().out() << "<a href='" << url("/") << "'>Go back</a>";
    }
        void welcome()
    {
        response().out() <<
            "<h1> Welcome To Page with links </h1>\n"
            "<a href='" << url("/number",1)  << "'>1</a><br>\n"
            "<a href='" << url("/number",15) << "'>15</a><br>\n"
            "<a href='" << url("/smile") << "' >:-)</a><br>\n";
    }
    virtual void main(std::string url);
};

void hello::main(std::string /*url*/)
{
    response().out() <<
       "<html>\n"
        "<body>\n"
       "  <h1>Hello World</h1>\n"
       "<center><br>This is a simple C++ website</br></center>"
    "</body>\n"
      "</html>\n";

}

int main(int argc,char ** argv)
{
    try {
        cppcms::service srv(argc,argv);
        srv.applications_pool().mount(
      cppcms::applications_factory<hello>()
    );
            srv.run();
    }
    catch(std::exception const &e) {
        std::cerr << e.what() << std::endl;
 }
}'

任何帮助表示赞赏。

【问题讨论】:

    标签: c++ cppcms


    【解决方案1】:

    看起来original tutorial 忘记提及您需要删除它在第一个教程中创建的virtual void main 函数。如果您删除它,它会按预期工作。

    这里是固定的源代码:

    #include <cppcms/application.h>
    #include <cppcms/service.h>
    #include <cppcms/http_response.h>
    #include <cppcms/url_dispatcher.h>
    #include <cppcms/url_mapper.h>
    #include <cppcms/applications_pool.h>
    #include <iostream>
    #include <stdlib.h>
    
    class hello : public cppcms::application {
    public:
        hello(cppcms::service &srv) :
                cppcms::application(srv)
        {
        dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
        mapper().assign("number","/number/{1}");
    
        dispatcher().assign("/smile",&hello::smile,this);
        mapper().assign("smile","/smile");
    
        dispatcher().assign("",&hello::welcome,this);
        mapper().assign("");
    
        mapper().root("/hello");
        }
            void number(std::string num)
        {
            int no = atoi(num.c_str());
            response().out() << "The number is " << no << "<br/>\n";
            response().out() << "<a href='" << url("/") << "'>Go back</a>";
        }
            void smile()
        {
            response().out() << ":-) <br/>\n";
            response().out() << "<a href='" << url("/") << "'>Go back</a>";
        }
            void welcome()
        {
            response().out() <<
                "<h1> Welcome To Page with links </h1>\n"
                "<a href='" << url("/number",1)  << "'>1</a><br>\n"
                "<a href='" << url("/number",15) << "'>15</a><br>\n"
                "<a href='" << url("/smile") << "' >:-)</a><br>\n";
        }
    
    };
    
    int main(int argc,char ** argv)
    {
        try {
            cppcms::service srv(argc,argv);
            srv.applications_pool().mount(
          cppcms::applications_factory<hello>()
        );
                srv.run();
        }
        catch(std::exception const &e) {
            std::cerr << e.what() << std::endl;
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      相关资源
      最近更新 更多