开发者

Converting Java program to Spring Framework

I have a short program that takes a string from a console prompt, in this format:

1=Harry_2=Male_3=54_4=Blonde_5=French_6=Teacher

and prints it like so:

1 Name Harry

2 Gender Male

3 Age 54

4 Hair Blonde

5 Nationality French

6 Occupation Teacher

However, my next aim is to take this and recreate it as a开发者_高级运维 program that operates from a web browser. I've read the documentation for the Spring Framework and got a demo program running, but I'm at a loss as to how to begin taking my script and fitting it to the framework.

Ideally, I'd like to have a single page with an input box which prints the results underneath.

Any advice on how to begin this process would be great.

Oh, the code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class maptest { 
    public static void main(String args[]) throws IOException { 
        
        Map<String, String> tagMap = new HashMap<String, String>(); 
        tagMap.put("1","Name");
        tagMap.put("2","Gender");
        tagMap.put("3","Age");
        tagMap.put("4","Hair");
        tagMap.put("5","Nationality");
        tagMap.put("6","Occupation");

        String input; 
        BufferedReader reader;
        reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Insert raw data: ");
        input = reader.readLine();
        
        String Msg = input;
        String[] params = Msg.split("_");  
        for (String p : params) {     
            String[] nv = p.split("=");     
            String name = nv[0];     
            String value = nv[1];     
            System.out.println(nv[0] + " " + tagMap.get(nv[0]) + " " + nv[1]);} 
        
    } 
}


To mimic this in a Spring webapp, you'll want to create a Controller class that takes the input from the HTML form and transforms it to the output you like. See Implementing Controllers in the reference documentation.


You have many options to process the input (e.g., receive it as part of the URL, structured in a form submit) and many options to render the result (typically using HTML).

You can follow the Spring-MVC 3 Showcase tutorial for a simple controller and the basic notion of mapping information from requests to controllers, and from controllers to views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜