我正在尝试使用Spring MVC构建RESTful API.我正在拍摄干净且易于管理的代码,其中包结构遵循url结构.
所以这就是我所拥有的:
// com.test.api.library
@RequestMapping("/library/{libraryId}")
public Library getLibrary(@PathVariable long libraryId) {
return service.getLibraryById(libraryId);
}
Run Code Online (Sandbox Code Playgroud)
// com.test.api.library.book
@RequestMapping("/library/{libraryId}/book/{bookId}")
public Book getBook(@PathVariable long libraryId, @PathVariable long bookId) {
Library library service.getLibraryById(libraryId);
return library.getBookById(bookId);
}
Run Code Online (Sandbox Code Playgroud)
虽然这很有效,但我觉得在所有继承的@RequestMappings中重复"/ library/{libraryId}"会很麻烦且容易出错,/库很可能是API的一个重要部分的根,它应该写成曾经重复使用而不是随处写.
我想把书类改写成这样的东西:
// com.test.api.library.book
@RequestMapping("/book/{bookId}")
public Book getBook(@PathVariable long bookId) {
// long libraryId magically given to me from the library-class's getLibrary()
Library library service.getLibraryById(libraryId);
return library.getBookById(bookId);
}
Run Code Online (Sandbox Code Playgroud)
春天有什么方法可以帮助我吗?我可以使用普通的java继承,spring注释或任何其他帮助我不写"/ library/{libraryId}"作为我写过的每个url的一部分.
#include <iostream>
struct slow_tag {};
struct fast_tag {};
template <typename T>
struct traits
{
typedef slow_tag tag;
};
template <>
struct traits<int>
{
typedef fast_tag tag;
};
template <typename T>
void work_dispatch(const slow_tag)
{
std::cout << "Slow function" << std::endl;
}
template <typename T>
void work_dispatch(const fast_tag)
{
std::cout << "Fast function" << std::endl;
}
template <typename T>
void work_dispatch(const T)
{
work_dispatch(typename traits<T>::tag());
}
int main()
{
std::cout << "Starting my program" << std::endl; …Run Code Online (Sandbox Code Playgroud) 我正在尝试提交输入数据,但我想提交数据<textarea>而不是<input>.我原来的代码:
<form action="/index/output" method="POST">
<input type="text" name="text_box" id="t">
</form>
<script>
$('#t').keyup(function(){
$.ajax({
url : '/index/output',
data : {
text_box : $('input:text').val()
},
success : function(html) {
$('#result').html(html);
}
})
})
</script>
<div id="result"> </div>
Run Code Online (Sandbox Code Playgroud)
我换<input>了<textarea>
<form action="/index/output" method="POST">
<textarea name="text_box" id="t"> </text>
</form>
Run Code Online (Sandbox Code Playgroud)
但它效果不好......
我的ajax代码有问题吗?