2017-08-11 6 views
1

私はCppCMSを使いこなしています。静的な "Hello World"が使えます。しかし、私は仕事にURLマッピングを取得本当の苦労を抱えています。私は愚かで何かが明らかでないと確信しています。CppCMS 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; 
} 
}' 

は、ここでは、コードです。

答えて

0

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; 
} 
} 
+0

ありがとうございました! :-) – X01