[英]Java Spring Friendly Url mapping issues
I've been trying to implement friendly url mapping in my first Java spring site. 我一直在尝试在我的第一个Java spring站点中实现友好的url映射。 I've been following this tutorial.
我一直在关注本教程。 http://outbottle.com/spring-3-web-mvc-friendly-url-using-requestmapping-variable-uri/
http://outbottle.com/spring-3-web-mvc-friendly-url-using-requestmapping-variable-uri/
My current mapping works well with id's as parameters. 我当前的映射可以很好地将id作为参数。 localhost:8080/user?id=1312321321
本地主机:8080 / user?id = 1312321321
/*
* User
*/
@RequestMapping(method=RequestMethod.GET, value={"/user","/user/{id}"})
public ModelAndView profileDisplay(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
ServiceSerlvet.appendSesssion(request);
//get search ALL users
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", new ObjectId(id));
List<DBObject> searchResponse = PersonController.searchUsers(searchQuery);
//System.out.println("response from search user method: "+searchResponse);
return new ModelAndView("user", "people", searchResponse);
}
My web xml currently looks like this... its working but is it correct to write out various url mapping like this? 我的Web xml当前看起来是这样的……它可以正常工作,但是写出这样的各种URL映射是否正确? I take it the * is a wild card to allow say /user/22222?
我认为*是通配符,允许说/ user / 22222?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Spring3MVC</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
<url-pattern>/gallery/*</url-pattern>
<url-pattern>/galleryupload/*</url-pattern>
<url-pattern>/delete/*</url-pattern>
<url-pattern>/edit/*</url-pattern>
<url-pattern>/search/*</url-pattern>
<url-pattern>/members/*</url-pattern>
<url-pattern>/profile/*</url-pattern>
<url-pattern>/messages/*</url-pattern>
<url-pattern>/index/*</url-pattern>
<url-pattern>/login/*</url-pattern>
<url-pattern>/logout/*</url-pattern>
<url-pattern>/register/*</url-pattern>
<url-pattern>/user/*</url-pattern>
<url-pattern>/jsoninterests/*</url-pattern>
<url-pattern>/jsonlocations/*</url-pattern>
<url-pattern>/jsonmembers/*</url-pattern>
<url-pattern>/jsonuniqueuser/*</url-pattern>
</servlet-mapping>
</web-app>
When I try and adapt my code to take just a name like this localhost:8080/user/john 当我尝试修改我的代码时,只采用这样的名称:localhost:8080 / user / john
it breaks - but I am unsure how to set the mapping in the web.xml, do I set the mapping like this in web.xml? 它会中断-但是我不确定如何在web.xml中设置映射,是否可以在web.xml中设置这样的映射?
@RequestMapping(value="/user/{id}", method= RequestMethod.GET)
public ModelAndView profileDisplay(
@PathVariable(value="id") String id,
HttpServletRequest request,
HttpServletResponse response
) throws UnknownHostException, MongoException {
ServiceSerlvet.appendSesssion(request);
//get search ALL users
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", new ObjectId(id));
List<DBObject> searchResponse = PersonController.searchUsers(searchQuery);
//System.out.println("response from search user method: "+searchResponse);
return new ModelAndView("user", "people", searchResponse);
}
Normally I map every request to the dispatcher servlet in web.xml. 通常,我将每个请求映射到web.xml中的调度程序servlet。
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then in your Controllers use @RequestMapping annotations to define the more granular mappings: 然后在Controller中,使用@RequestMapping批注定义更精细的映射:
@RequestMapping("/user/{id}")
public ModelAndView profileDisplay(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.