Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:41:18+00:00 2026-05-26T08:41:18+00:00

I want to create a custom finder (being a web development veteran but a

  • 0

I want to create a custom finder (being a web development veteran but a complete first-timer at Java, Spring, AND Roo).

The first method below is from my “Asset” view controller class. It’s what happens on your first hit to the asset administration page–it comes back with a view and a batch of the currently active assets. It works fine:

@RequestMapping("/assets")
public ModelAndView listAssets() {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("admin/asset");
    Collection<com.myapp.model.Asset> assets = com.myapp.model.Asset.findAllAssets() ;
    mav.addObject("assets", assets);    
    return mav;
}

So now I want to have a “POST” request to that URL accept the submission of the asset search form, which contains fields for several of the key properties of the Asset object.

(Right now I’m coding that finder in my view controller, and I know ultimately I’ll want to push that down to the model class. For now I’m just working it here for convenience sake. Also, I know I don’t have to give the fully qualified name of the object, I’ve newbishly named my controller the same name as the model it’s talking to, so until I sort that out, I’m working around it.)

I’ve borrowed some code from some Roo finders I generated, and ended up with:

@RequestMapping(value = "/assets", method = RequestMethod.POST)
public ModelAndView searchAssets(
            @RequestParam("category") String category,
            @RequestParam("year") String year,
            @RequestParam("manufacturer") String manufacturer,
            @RequestParam("drive_type") String driveType,
            @RequestParam("subcategory") String subcategory,
            @RequestParam("serial") String serial,
            @RequestParam("listing_type") String listingType,
            @RequestParam("hours") String hours,
            @RequestParam("model") String model,
            @RequestParam("mileage") String mileage  ) {
    ModelAndView mav = new ModelAndView();
    mav.setViewName("admin/asset");


    EntityManager em = com.myapp.model.Asset.entityManager();
    TypedQuery<Asset> q = em.createQuery("SELECT o FROM Asset AS o ", Asset.class);
    Collection<Asset> assets =  q.getResultList();
    mav.addObject("assets", assets);

    return mav;
}

So the questions are:

1) Is there a way to get a collection of my parameters, rather than baking them into the method signature? Because I sort of hate that.

2) Is there a way to iterate over my parameters and generate the WHERE clause of my query string that way? I’d like to not have to know much about the fields I’m being given here–and I need to be able to handle that I mostly won’t have all of them. So I’d prefer to just build my query iteratively rather than declaratively, if that makes sense.

How would you go about building the select statement here?

EDIT (Solution):

@madth3 pointed me in the right direction here. Here’s what I ended up with, which I’m pretty proud of:

public static TypedQuery<Asset> findAssetsByWebRequest( WebRequest request) {

    ArrayList<String> wheres = new ArrayList<String>();

    Iterator<String> i = request.getParameterNames();

    while (i.hasNext()) {
        String fieldname = i.next();
        String value = request.getParameter(fieldname);

        if (value.length() == 0) {
            continue;
        }

        if (fieldname.equals("manufacturer") || fieldname.equals("model") ) {

            value = value.replace('*', '%');
            if (value.charAt(0) != '%') {
                value = "%" + value;
            }
            if (value.charAt(value.length() - 1) != '%') {
                value = value + "%";
            }
            wheres.add(" o." + fieldname + " LIKE '" + value + "'");
        }
        else if (fieldname.contains("min")) {
            fieldname = fieldname.replace("min", "").toLowerCase();
            wheres.add(" o." + fieldname + " >= '" + value + "' ");
        }
        else if (fieldname.contains("max")) {
            fieldname = fieldname.replace("max", "").toLowerCase();
            wheres.add(" o." + fieldname + " <= '" + value + "' ");

        }
        else {
            wheres.add(" o." + fieldname + " = '" + value + "' ");
        }
    }


    String query = "SELECT o FROM Asset AS o ";
    if (wheres.size() > 0) {
        query += " WHERE ";
        for (String clause: wheres) {
            query += clause + " AND "; 
        }
        query += " 1 = 1 ";
    }


    EntityManager em = Asset.entityManager();
    TypedQuery<Asset> q = em.createQuery(query, Asset.class);

    return q;
}

It obviously needs to be parameterized to avoid SQL injection attack, but it’s cool that it (nearly) doesn’t have to know anything about the data being given it, that it’ll just assemble a query from the fields it was given. It does need to know which fields it’s doing what with–which fields are “like” versus “equals”, how to handle “min” and “max” fields that define a range, etc. But that’s not so bad.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:41:19+00:00Added an answer on May 26, 2026 at 8:41 am

    Well, going the old fashioned way…
    The Servlets standard in Java defines object Request where there is Map (hash, dictionary) with the parameters, so you could access them something like

    public ModelAndView searchAssets(HttpRequest request) {
        StringBuilder builder = new StringBuilder();
        for (String param : request.getParameterNames()) {
            value = request.getParameter(param);
            builder.append(param);
            builder.append("="); // or LIKE
            builder.append("\'" + value "\'");
            builder.append(" AND ");
        }
        // deleting the last " AND "
        builder.delete(builder.length-5, builder.length);
        // In the entity create a method that executes a query with the string
        // If the parameter names are column names then you'd have to use a nativeQuery
        // You'd have to look it up in JPA
       List<Asset> list = Asset.search(builder.toString());
       // put the list in the ModelAndView
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a custom control in C#. But every time I have
I want to create custom tag, but i get XML parsing error on JSPVersion
I want to create a custom annotation (using Java) which would accept other annotations
I want to create custom Textbox control in asp.net.But i have one doubt when
I want to create a custom filter Web Part. I found two ways of
i want to create a custom view class. But I get an error by
I want to create custom AlertDialog, but without AlertDialog.Builder. I set ListView as content
I want to create custom ImageButton, but to work like on/ off button. On
impress.js is a new web presentation written using Javascript. I want to create custom
I want to create a custom callout bubble on MKMapView. But I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.