The view
Let's look at the view first. I'm using Velocity here and below you can only see a tiny bit of the file.<form method="post" action=""> <table> <tr> <td>Your name:</td> <td>#springFormInput("reservation.userName" "") </td> <td>#springShowErrors("" "") </td> </tr>This will create an input field with an ID userName.
HTML metadata
<link type="text/css" href="../css/humanity/jquery-ui-1.8.16.custom.css" rel="stylesheet" /> <link type="text/css" href="../css/basic.css" rel="stylesheet" /> <script type="text/javascript" src="../js/jquery-1.6.2.min.js"></script> <script type="text/javascript" src="../js/jquery-ui-1.8.16.custom.min.js"></script> <script> $(function() { $( "input:submit" ).button(); }); </script> <style> .ui-autocomplete-loading { background: white url('../images/ui-anim_basic_16x16.gif') right center no-repeat; } </style> <script type="text/javascript" src="../js/autocomplete.js"></script>Only the last line is non-standard. That's my custom JS file.
autocomplete.js
That's my custom JS file based heavily on the example jQuery UI have on their site.$(function() { $("#userName").autocomplete( { source : function(request, response) { $.ajax({ url : "names", dataType : "json", data : { term : request.term }, success : function(data) { response($.map(data.names, function(item) { return { label : item.label, value : item.label } })); } }); }, minLength : 1 }); });Let's explain it step by step. On windows load, the nameless function will run. Using jQuery selector I'm selecting the HTML element with ID userName. Then I'm calling the autocomplete() function on it that will turn it into an autocomplete input. It accepts many parameters; here I only use source and minLength because they are sufficient.
minLength specifies the content length threshold for back end query trigger. 1 means you only have to type 1 character to get suggestions.
source declares a function to handle response. That gives us the flexibility we need to handle our JSON response which looks like that:
{"names":[{"label":"Michael","value":"1"},{"label":"Mike","value":"2"},{"label":"Mikey","value":"3"}]}This bit:
$.map(data.names, function(item)… selects the names field from the response. Then, we have to select the label and value. Label is what will be displayed in the suggestion box. Value is what will be ultimately selected. In my case I chose to ignore the returned value (which is a number, as you can see) and just use the label field.
Back end - the controller
@Controller @RequestMapping("/names") public class NamesController { private static final Logger log = LoggerFactory .getLogger(NamesController.class); @RequestMapping(method = RequestMethod.GET) public String getNames(Model model, @RequestParam(required = false) String term) { log.debug("getNames(\"{}\")", term); model.addAttribute("names", Arrays.asList(names)); return "jsonNames"; } private LabelValueTo[] names = new LabelValueTo[] { new LabelValueTo("Michael", "1"), new LabelValueTo("Mike", "2"), new LabelValueTo("Mikey", "3") }; }I have already written a bit about Spring and JSON so I will not comment that in great detail. Notice that I'm returning a view called jsonNames. What happens with it next?
Back end - view resolvers
I have these 2 view resolvers declared (there is more configuration to it - to get it download the source).<bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="order" value="0" /> <property name="location" value="/WEB-INF/views.xml" /> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver"> <property name="order" value="1" /> <property name="cache" value="true" /> <property name="prefix" value="/WEB-INF/vm/" /> <property name="suffix" value=".vm" /> <property name="exposeSpringMacroHelpers" value="true" /> </bean>And the referenced view.xml file:
<bean name="jsonNames" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
I am trying to implement autocomplete for text box using spring 3. I refer your code for it. but when I enter key into my text box then console shows me error "Unexpected character < in JSON at position 6". I put sys out into my controller but my console doesn't showing me anything therefore I am confuse whether my request is heating to controller or not.
ReplyDeletePlease help me.
Thanks,
Viraj
The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. machine learning projects for final year In case you will succeed, you have to begin building machine learning projects in the near future.
DeleteProjects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.
Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.
The Nodejs Projects Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
great
ReplyDeleteHi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog.
ReplyDeleteReally very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from javascript and jquery training in chennai .
or learn thru Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry. javascript and jquery training in chennai