简体   繁体   中英

cppcms - why is hello world with url mapping not working?

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.

You need to setup the mapping in your hello constructor, as shown in both the hello world example and the link you posted in your answer.

In particular, this part:

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

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

This maps the default route of the hello application to the "welcome" method. If you don't set a mapping, it's going to default to main.

I do not personally know much about this library, however it looks like in the line:

cppcms::applications_factory<hello>()

Your assosating the server with, and instantaiteing some sort of hello class. However I

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM