Announcement Announcement Module
Collapse
No announcement yet.
How to set "get" parameters? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set "get" parameters?

    Hi. How can I set some "get" parameters ?
    In php I can do that like example.com/test.php?ppp=qqq but in spring it doesn't work.

    I've tried to do this like this:
    Code:
    <html>
      <head><title>springframework.pl</title></head>
      <body>
        <h1>Categories:</h1>
        <table>
        	<tr><td>Name</td><td>active</td><td>options</td></tr>
        <c:forEach items="${categories}" var="category">
        	<tr>
          		<td><c:out value="${category.name}"/></td>
          		<td><c:out value="${category.active}"/></td>
          		<td><a href="./delete-category.htm?id=${category.id}">delete</a></td>
          	</tr>
        </c:forEach>
        </table>
      </body>
    </html>


    Code:
    package web;
    
    import org.springframework.web.servlet.mvc.Controller;
    import org.springframework.web.servlet.view.RedirectView;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import dao.*;
    import domain.*;
    
    import java.io.IOException;
    import java.util.*;
    
    public class DeleteCategoryController implements Controller {
    
    	private CategoryDao categoryDao;
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
        	
        	Integer id = (Integer) request.getAttribute("id");
        	Category category = this.categoryDao.getCategory(id);
        	this.categoryDao.delete(category);
        	
        	return new ModelAndView(new RedirectView("category"));
        }
    
        public void setCategoryDao(CategoryDao categoryDao){
        	this.categoryDao = categoryDao;
        }
    }
    I got an error:

    Code:
    org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: id to load is required for loading
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:583)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    I can't get this parameter ...

    How can I do that ?

  • #2
    Well because the are PARAMETERS instead of ATTRIBUTES.

    Code:
    Integer id = (Integer) request.getAttribute("id");
    should be

    Code:
    Integer id = Integer.valueOf(request.getParameter("id"));

    Comment

    Working...
    X