简体   繁体   中英

Java Spring Friendly Url mapping issues

I've been trying to implement friendly url mapping in my first Java spring site. I've been following this tutorial. http://outbottle.com/spring-3-web-mvc-friendly-url-using-requestmapping-variable-uri/

My current mapping works well with id's as parameters. localhost: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? I take it the * is a wild card to allow say /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

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?

@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.

  <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:

    @RequestMapping("/user/{id}")
    public ModelAndView profileDisplay(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id", required=false) String id
    ) throws UnknownHostException, MongoException {
        ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM