How to use angularjs with struts 1.x

故事扮演 提交于 2019-12-01 16:10:56

问题


I'm new to angularjs and client side stuff and I'm adding a new page to an old application that uses struts 1.3 as its framework. I would like to use angularjs for the front end.

I know how to return json from the action class by writing the json to the front end and returning null for the action forward. However, I'm unclear how I would populate the scope variables within the controller after the get. If I use a get in the controller and get json back, how does my actionform fit into all this? Is it useful at all? Can I have a get and post for the same controller if I want to send new values back to my action? And, can I have variables like: $scope.items={} $scope.items.name="" $scope.items.email="" And then just send json.stringify (items) as my data in the post if I can't use the form somehow? Thanks for your time. I haven't found much information using angularjs with struts 1.3 so far.

Answer: I'll try to answer as best as possible what conclusion I came to but my questions above were very vague since I didn't really know what I was talking about and my position with the company I worked for has ended so I no longer have access to that code. What I meant to ask earlier was what am I going to do with the action form that I usually use for struts actions and how am I going to get data from the front end to my action class during a post? I found out that my usual action form was useless for what I wanted to do so I got rid of it and wrote a json object during the get that would be modified on the front end and passed back to another action when I did the post. This is done like a normal post to 'whateverAction.do' but I had to configure the data I was sending in the post and name it something. I then picked up the modified json object by using request.getParameter("jsonObjName") and parsed it to different LinkedHashMap objects for each object in the json object. I think you can use the JSONObject classes instead of LinkedHashMap to parse if you are using JavaEE but I was using SE so I didn't have access to those in this project. Here a link to another page I used for the post configurations:

How to get the data in Struts from AngularJS post


回答1:


On the java side you can also try to use Gson library that allows you to parse string to and from json, but struts1 generally won't fit well with angularjs, if you remove the form on java side, than makes no sense to keep struts1. And if no possibility to remove struts1, than just keep struts1 for existing functionality and start using struts2 for new screens where angularjs is applied. It will be much easier cause the json from angular post will automatically be bound to the bean object that has to match to json properies format and name.



来源:https://stackoverflow.com/questions/28306645/how-to-use-angularjs-with-struts-1-x

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!