Java springmvc的应用程序路由
我正在寻找一种在我的WebApp上管理路线使用的方法。 基本上,我有三个地方可以共享路由器模式。 我可以通过表达式语言将此模式发送给我的视图Java springmvc的应用程序路由,java,design-patterns,spring-mvc,url-routing,Java,Design Patterns,Spring Mvc,Url Routing,我正在寻找一种在我的WebApp上管理路线使用的方法。 基本上,我有三个地方可以共享路由器模式。 我可以通过表达式语言将此模式发送给我的视图 @Controller public class LoginRuasController { @RequestMapping("/system/index") public String logout(ModelMap model, HttpSession session) { return "system/inde
@Controller
public class LoginRuasController
{
@RequestMapping("/system/index")
public String logout(ModelMap model, HttpSession session)
{
return "system/index";
}
@RequestMapping("/system/logout")
public String logout(ModelMap model, HttpSession session)
{
session.setAttribute("xxx", null);
return "redirect:/system/login";
}
}
模式:
/system/index
system/index
redirect:/system/login
观点:
<a href="#{Routes.newuser}">Triple X</a>
最初,RequestMapping请求一个常量值,因此生成
使用静态返回实现Route类的问题。
有什么解决方案吗?我找到了一个解决方案,如下所示: 1) 我创建了一个Routes类
public class Routes {
private static HashMap<String, String> routes;
public static final String host = "/mywebapp";
public static final String home = "/home";
public static final String login = "/login";
public static final String logout = "/logout";
private static void setRoutes()
{
if(routes == null)
{
routes = new HashMap<String, String>();
routes.put("host", host);
routes.put("home", host + home);
routes.put("entrar", host + entrar);
routes.put("sair", host + sair);
}
}
public static HashMap<String, String> getRoutes()
{
setRoutes();
return routes;
}
public static String getRoute(String destin)
{
setRoutes();
return routes.get(destin);
}
}
3) 我设置要在视图上使用的路线
public abstract class AbstractController {
protected void preRender(ModelMap model) {
model.addAttribute("routes", Routes.getRoutes());
}
}
4) 现在可以在视图上使用
<body>
<p>Mary is singing.</p>
<p><a href="${routes.home}">Home</a></p>
</body>
玛丽正在唱歌
查看GitHub上的SpringMVC路由器项目:
这是SpringMVC的播放路线文件的一个实现对不起,很难理解,您所说的
应用程序路线
到底是什么意思。注意:在can或can not put to之后,即正确的形式是can share,not can to share。代码的简单维护是我的目标。请尝试重新表述您的问题。你的目标应该更加明确。每个优秀的开发人员的目标都是创建可维护的代码——请用更具体的例子来描述您的问题。似乎您所要求的是一种基于其他因素的引用路由的方法,例如控制器/方法名称(如Rails允许的)。“这是对的吗?”帕西法尔是的,差不多是这样。我想有一个中心路线管理。示例:在路由上可能遇到对“”的所有引用。在Routes类中,我希望有一个基于param传递的“destin”返回它的方法。所以,通过Routes.get(“system_users”)调用,然后它将返回相应的url。“最新提交2015年2月”这个老问题,我知道,但对于未来的搜索者:您也可以使用@ControllerAdvice
和@modeldattribute
的组合,将对routes类的引用添加到与每个控制器关联的模型中。您有示例吗?
<body>
<p>Mary is singing.</p>
<p><a href="${routes.home}">Home</a></p>
</body>