c++ - CppCMS URL 映射问题

标签 c++ cppcms

我正在尝试使用 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;
 }
}'

感谢任何帮助。

最佳答案

看起来像 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;
 }
}

关于c++ - CppCMS URL 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644592/

相关文章:

c++ - 字符串中的最大重复词

c++ - Open GL Create 4 Views Like 3ds max or maya

c++ - std::string.find_first_not_of,意外的返回值

c++ - openCL平台/设备查询

linux - 在 CppCms 中更新模板而不重新启动应用程序

c++ - 如何使用 lower_bound 将值插入排序 vector

c++ - 在cppcms GET或POST请求中显示参数

mysql - 连接池 : How to deal with prepared statements?

c++ - CppCMS URL 调度和映射失败。代码不完整?

c++ - cppcms 大量未解析的外部符号