2017-08-11 113 views
1

我正在玩CppCMS,我已經有了靜態的「Hello World」來工作。不過,我真的很難讓URL映射工作。我敢肯定我很愚蠢,錯過了一些明顯的東西。CppCMS URL映射問題

我遇到的問題是網址似乎不工作。當我嘗試訪問:8080/home/smile時,我只收到默認的「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"; 
    } 
    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