gpt4 book ai didi

c++ - CppCMS URL 映射问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:00 25 4
gpt4 key购买 nike

我正在尝试使用 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;
}
}'

感谢任何帮助。

最佳答案

看起来像 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;
}
}

关于c++ - CppCMS URL 映射问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45644592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com