1

我正在玩 CppCMS,我已经让静态的“Hello World”工作了。但是,我很难让 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;
 }
}'

任何帮助表示赞赏。

4

1 回答 1

0

看起来原始教程忘记提及您需要删除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;
 }
}
于 2017-08-13T10:06:27.500 回答