Jquery Spring mvc 3在“之后追加内容”#&引用;在url中?例如www.example.com/example#ui-tabs-4
这是我的控制器代码Jquery Spring mvc 3在“之后追加内容”#&引用;在url中?例如www.example.com/example#ui-tabs-4,jquery,spring,jquery-ui,spring-mvc,Jquery,Spring,Jquery Ui,Spring Mvc,这是我的控制器代码 @RequestMapping(value = "example.htm", method = RequestMethod.GET) public ModelAndView showMasterForm(ModelMap model) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); MasterJDBCTemplate dao = (
@RequestMapping(value = "example.htm", method = RequestMethod.GET)
public ModelAndView showMasterForm(ModelMap model)
{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
MasterJDBCTemplate dao = (MasterJDBCTemplate)context.getBean("masterJDBCTemplate");
List<String> stateList = dao.getStateList();
model.addAttribute("stateList", stateList);
return new ModelAndView("admin/master/example","command" ,new BranchChannelMaster());
}
@RequestMapping(value=“example.htm”,method=RequestMethod.GET)
公共模型和视图showMasterForm(模型地图模型)
{
ApplicationContext上下文=新的ClassPathXmlApplicationContext(“Beans.xml”);
MasterJDBCTemplate dao=(MasterJDBCTemplate)context.getBean(“MasterJDBCTemplate”);
List stateList=dao.getStateList();
model.addAttribute(“状态列表”,状态列表);
返回新的ModelAndView(“admin/master/example”,“command”,new BranchChannelMaster());
}
这会将我带到一个类似www.example.com/example
的URL。要转到www.example.com/example#ui-tabs-4
,我可以做些什么
我之所以出现这种情况,是因为我正在使用jQuery ui,现在我想以表格格式显示结果。HTML中的“#”字符通常表示页面锚定。它允许你在某个地方设置一个锚,然后通过页面上的链接跳转到锚。例如:
<html>
<body>
<a href="#news">Go to News</a>
<!-- ...some other HTML -->
<a name="news" />
<body>
</html>
理论上,这将改变您的方法,以接受您建议的URL模式。如果要将“#”后面的内容作为变量,可以执行以下操作:
@RequestMapping(value = "example.htm#{tabName}", method = RequestMethod.GET)
public ModelAndView showMasterForm(@PathVariable String tabName, ModelMap model) {
@RequestMapping(value = "example.htm#{tabName:[a-zA-Z]+}", method = RequestMethod.GET)
public ModelAndView showMasterForm(@PathVariable String tabName, ModelMap model) {
这将创建一个path变量,该变量的值为在#之后传入的任何内容,浏览器将其传递给服务器
另外,请记住@RequestMapping可以接受正则表达式,因此如果您想让它接受“example.html#anything”,可以执行以下操作:
@RequestMapping(value = "example.htm#{tabName}", method = RequestMethod.GET)
public ModelAndView showMasterForm(@PathVariable String tabName, ModelMap model) {
@RequestMapping(value = "example.htm#{tabName:[a-zA-Z]+}", method = RequestMethod.GET)
public ModelAndView showMasterForm(@PathVariable String tabName, ModelMap model) {
这将导致它只匹配一个或多个字符a-z或a-z
我仍然不认为它会起作用,因为我认为浏览器不会发送“#”之后的任何内容
更新
OP似乎想返回一个带有#锚的链接,这样UI就可以正确地跳转到正确的位置
我认为最好的方法是返回一个重定向视图
:
@RequestMapping(value = "example.htm", method = RequestMethod.GET)
public ModelAndView showMasterForm(ModelMap model)
{
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
MasterJDBCTemplate dao = (MasterJDBCTemplate)context.getBean("masterJDBCTemplate");
List<String> stateList = dao.getStateList();
model.addAttribute("stateList", stateList);
return new ModelAndView(new RedirectView("admin/master/example#ui-tabs-4", true));
}
@RequestMapping(value=“example.htm”,method=RequestMethod.GET)
公共模型和视图showMasterForm(模型地图模型)
{
ApplicationContext上下文=新的ClassPathXmlApplicationContext(“Beans.xml”);
MasterJDBCTemplate dao=(MasterJDBCTemplate)context.getBean(“MasterJDBCTemplate”);
List stateList=dao.getStateList();
model.addAttribute(“状态列表”,状态列表);
返回新的ModelAndView(新的重定向视图(“admin/master/example#ui-tabs-4”,true));
}
另一种方法是返回字符串而不是ModelAndView,并返回字符串“redirect:admin/master/example#ui-tabs-4”
参考文献: