2

I am having trouble with cppcms hello world example with url mapping.

I am having trouble with understanding this part:

int main(int argc,char ** argv)
{  
    try {
        cppcms::service srv(argc,argv);


        srv.applications_pool().mount(
                cppcms::applications_factory<hello>() //i do not understand this part
        );

        srv.run();
    }
    catch(std::exception const &e) {
        std::cerr << e.what() << std::endl;
    }
}

In tutorial said /hello - welcome function would be called but that is not what is happening. This method is called instead:

void hello::main(std::string /*url*/)
{
    response().out() <<
        "<html>\n"
        "<body>\n"
        "  <h1>Hello World</h1>\n"
        "</body>\n"
        "</html>\n";
}

Welcome method look like this and it is defined in a scope of hello class:

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";
}

I need some answers if you can help me. I am just trying to understand so if you could point me to right direction it would be great.

4

2 回答 2

3

您需要在 hello 构造函数中设置映射,如 hello world 示例和您在答案中发布的链接所示。

特别是这部分:

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

mapper().root("/hello");  

这会将 hello 应用程序的默认路由映射到“welcome”方法。如果您不设置映射,它将默认为 main。

于 2013-10-08T20:03:57.900 回答
0

我个人对这个库了解不多,但它看起来像这样:

cppcms::applications_factory<hello>()

您将服务器与某种hello类进行关联并实例化。但是,我

于 2013-10-08T22:50:51.623 回答