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.

crashmstr
  • 28,043
  • 9
  • 61
  • 79
pregmatch
  • 2,629
  • 6
  • 31
  • 68

2 Answers2

3

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.

Gerald
  • 23,011
  • 10
  • 73
  • 102
0

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