2

最近、Spring MVC フレームワークを使い始めました。ネットでたくさんのチュートリアルを読みながら、私は多くの進歩を遂げました。

私の申請の背景 -

フォームで提供された詳細を使用して、(Tomcat に既にデプロイされている) 別のサービスへの REST URL 呼び出しを行う必要があります。そのため、写真に示すように、JSP を使用してフォームを作成しました。その内容は次のようなものです。フォーム エントリから URL を作成して REST URL 呼び出しを行い、その URL の応答を表示する方法がわかりません。次の画面。

したがって、上記のフォームで と を書いた場合User Id as 1000012848(checkbox is selected (means true) for Debug Flag一般Attribute Name I have selected first row的には 3 つすべてを選択することもできます) とMachine Name is localhosturlPort Number is 8080は次のようになります。

http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848,debugflag=true/host.profile.ACCOUNT

したがって、フォーム エントリから作成するすべての URL で、以下の行は常に同じ場所にあり、その後、各フォーム エントリが追加され始めます。

service/newservice/v1/get/

上記の URL を作成した後、送信をクリックするとすぐに、上記の URL が呼び出され、URL から取得した応答が次の画面 (result.jsp ファイル) に表示されます。それを行う方法がわかりませんか?以下は、私が作成した私のファイルです。誰かが私の問題を解決するのを手伝ってくれますか? この問題を解決するには、どのコードを変更する必要がありますか?

student.jsp ファイル (フォームを作成するファイル)

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<res:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="layout" content="main" />
    <title>First Tutorial</title>
</res:head>
<res:body>

    <form:form method="POST" action="/_hostnewapp/addStudent">
        <table>
            <tr>
                <td><form:label path="userId">User Id</form:label></td>
                <td><form:input path="userId" /></td>
            </tr>
            <tr>
                <td>Debug Flag :</td>
                <td><form:checkbox path="debugFlag" /></td>
            </tr>
            <tr>
                <td>Attribute Name</td>
                <td><form:select path="attributeNames" items="${attributeNamesList}"
                        multiple="true" /></td>
            </tr>
<!--        <tr>
                <td>Environment</td>
                <td><form:checkboxes items="${environmentList}"
                        path="environments" /></td>
            </tr>
 -->            
            <tr>
                <td><form:label path="machineName">Machine Name</form:label></td>
                <td><form:input path="machineName" /></td>
            </tr>
            <tr>
                <td><form:label path="portNumber">Port Number</form:label></td>
                <td><form:input path="portNumber" /></td>
            </tr>

            <tr>
                <td colspan="2"><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>

</res:body>
</html>

result.jsp ファイル (その URL にアクセスした後に結果を表示するために使用します)

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<res:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="layout" content="main" />
    <title>HostDomain</title>
</res:head>
<res:body>

    <h2>Response after submitting the result</h2>
    // Not sure what I need to add here to show the result after hitting the url

</res:body>
</html>

コントローラークラス-

@Controller
public class SampleRaptorController {

    @RequestMapping(value = "/student", method = RequestMethod.GET)
    public ModelAndView student() {
        return new ModelAndView("student", "command", new Student());
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent(@ModelAttribute("SpringWeb") Student student,
            ModelMap model) {
        model.addAttribute("userId", student.getUserId());

        return "result";
    }


    @ModelAttribute("attributeNamesList")
    public Map<String,String> populateSkillList() {

        //Data referencing for java skills list box
        Map<String,String> attributeNamesList = new LinkedHashMap<String,String>();
        attributeNamesList.put("ACCOUNT","host.profile.ACC");
        attributeNamesList.put("ADVERTISING","host.profile.ADV");
        attributeNamesList.put("SEGMENTATION","host.profile.SEG");  

        return attributeNamesList;
    }
}
4

1 に答える 1

5

RestTemplateSpringコンポーネントからRESTfulURLを呼び出すために使用できます

したがって、コントローラのメソッドは次のようになります

@Controller
public class SampleRaptorController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public String addStudent( @ModelAttribute("SpringWeb") Student student,
                    Model model){

        // Build URL
        StringBuilder url = new StringBuilder().
                        append("http://localhost:8080/service/newservice/v1/get").
                        append("?PP.USERID=" + student.getUserId).
                        append("&debugflag=" + student.isDebugFlag);// so on

        // Call service
        String result = restTemplate.getForObject(url.toString(), String.class);
        model.addAttribute("result", result);

        return "result";
    }

}

Spring構成では、restTemplateを次のように登録する必要があります。

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate"/>

詳細については、 RestTemplateのドキュメントを参照してください。

上記で十分です。

1つの提案..あなたのRESTfulURL(http://localhost:8080/service/newservice/v1/get/PP.USERID=1000012848, debugflag=true/host.profile.ACCOUNT)は本当にひどいです。問題を解決したら、RESTfulURLがどのように見えるかをグーグルで検索することをお勧めします。

乾杯、ビナイ

于 2013-01-21T09:20:57.320 に答える