Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am using Struts2 and an action implementing the Preparable interface.
When I submit the form, action properties are not being set in prepare() method of action. I get the values in the action method (eg. execute()), but they are empty in the prepare() method.
How can I get the properties set before running the prepare() method ?
In the default Interceptor Stack, the Prepare Interceptor runs before the Parameters Interceptor. That means that the injection of parameters has not yet occurred when the Prepare Interceptor executes the prepare() method. You need to move the Parameters Interceptor before the Prepare Interceptor, or to duplicate it, putting one declaration before the Prepare Interceptor.
There is a default Interceptor Stack created with that purpose, paramsPrepareParamsStack; read more here and here.
Note that this kind of problem is recurrent in Struts2, you need to understand how an Interceptor Stack works and which business every single Interceptor is taking care of.
For example, it happens when using ModelDriven, or when using Wildcard Mapping. And for sure it happens with your custom Interceptor if you put them in the wrong place.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
I have a mail sender method in my Spring Boot app and when I defining related endpoint in the Controller, I could not be sure what is the most proper request for that.
As I do not pass any parameter and the method does not return any content, I am not sure POST or GET is suitable for this. So, which request should I use?
You need to think of the intention behind the request. Since it is a MAIL request, you are intending to perform some action with this. Hence it would be advisable to use the POST method.
Here is a reference to the existing methods:
GET : The GET method requests a representation of the specified resource. Requests using GET should only retrieve data.
POST : The POST method submits an entity to the specified resource, often causing a change in state or side effects on the server.
Here is the Link for the MDN docs for this.
When you think about the future, there might be some data that you need to send for MAIL which you might not need now, hence using POST makes most sense.
Premised that the choice depends on your personal opinion and habit as developer, and on the specific purpose of your application.
However, in your specific case, I would follow the below logic:
Request is about "retrieving" email -> GET method is better
Request is about "sending" email -> POST method is better
So the logic is the following: as long as I ask the server to only "retrieve" information and I am not going to send any information to the server (i.e: coming from a form), i will always use GET.
On the other hand, when you also need to pass info to the server and it needs to apply some logic/operation, which might also affect some database, in that case the POST method works better.
Hope that answer your questions. Feel free to add more details, I will strive to help you further.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I know both ways would work, but I'm not sure which way would be more clean and flexible.
There is OrderService and it has the following methods:
searchOrders
setBookableFlag
mappingWithCarrier
Method 1: Invoking all the methods in controller
OrdersController:
public OrderCarrierDTO searchOrders(#PathVariable carrierGuid){
List<Order> orders = ordersService.searchOrders();
ordersService.setBookableFlag(orders, carrierGuid);
return orderService.mappingWithCarrier(orders, carrierGuid);
}
Method 2: Create a new method in Service and put all the callings in the method and invoke that new method from Controller:
OrdersController:
public OrderCarrierDTO searchOrders(#PathVariable carrierGuid){
return orderService.searchOrdersForCarrier(carrierGuid);
}
I prefer method 2
The controller should only be handling input / output. So the job of the controller is receive the carrierGuid and pass that to the service which then does its thing. The result of this (or an exception) comes back to the controller and then the controller returns the proper http code / body for it
I would suggest to use Method 2, Along with that you should ideally make all other three methods private and not accessible to the users of your service class.
That means the controller should not care if you call 3 or 30 methods inside your service, it should just know that if I call this method then it will get the output.
Since it's a good practice to maintain all the business logic in service and minimum code at controller layer, I would recommend the second approach. Assuming getting Orders for any Carrier is a frequent task, so maintaining a separate method (searchOrdersForCarrier()) in service for such tasks comes handy instead of calling multiple methods each time. if searchOrders() setBookableFlag() mappingWithCarrier() are also of reusable nature we can maintain them as separate methods and use them in searchOrdersForCarrier().
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am working on a Grails app that only consumes a third-party API and uses Firebase as a datastore. Because of this, I don't want to associate my controller methods with any views in the grails-app/views directory. I keep getting a ServletException: could not resolve view in servlet whenever any controller methods gets called back. How should I stop this from happening?
In Groovy if the last statement of the method is implicitly the return statement. Similarly, in Grails, a controller action method will expect a view with the same name as your method name if you do not explicitly render a view.
With that in mind, you can solve this in two ways.
Create an empty view that matches the method name with nothing in it.
Render an empty string as the last statement in your method as such.
class SomeController{
def index(){
// do stuff
render ""
}
}
If you do not wish to render anything, you likely need to render a status at the end of the method. You can do so like this: render(status: 200)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am new to Mockito and I have started to learn it. But I have some questions. Why do we need to use Mockito? As far as I know it is used to Mock(Create dummy object) and write the test cases before having actual running code. But, what if I want to test my already implemented code to check whether they are functioning properly or not. How would I test it using Mockito?
For instance, I have CRUD methods and I would like to test whether Create is functioning properly by actually inserting data in database using my Create method, similarly for others. Can we attain it using Mockito. If not, then do I need to write different testcases for them without using Mockito?
The Mock is used to each class or service you are using. The class under test should not be Mocked. Lets assume you are connecting to a remote service which is being built by one of your engineering team, and you are not familiar with its internal functionality but you know what requests and response it returns.
In that case, you can create a Mock of that Object, and defines it with set of responses returns in different situations. Each situation should get its own different test and for each response you should check separately the reaction of the code (you are working on).
Another great example is creating a limitation checks. Lets think of exception that might be thrown in some situations.
You can Mock the object that will throw the Exception which is simple(~2-3 line of test code if you are using Mock) and you can check how the code you have written reacts to that Exception. Without the Mock the throwing of an exception might be really complicated thing and not so easy to use if you are not familiar with the small details. And of course the Mock enables you to be on focus of the main functionality you are checking cause it make the checking time very very small. And that is a bless when time to market is a critical thing.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have played a bit with Spring Roo, now I am asking myself what is the Roo suggested way or best practice way to trigger an action after an object update.
Let me explain it with an example:
Assume I want to implement a web based Bug Tracker (I don’t want to do this, it is only an example). A bug tracker, is about Issues. Each Issue has a state (New, Confirmed, Assigned, In Progress, Resolved.), a title and some other fields.
The user has a web form where it can enter and update all fields (state, title, …). When the state of an issue switches from ‘In Progress’ to ‘Resolved’, the system should send an email to all persons that are interested in the bug (How this list of interested persons is maintained, is out of scope for this problem).
The problem that I have is: How to trigger the email sending process when the state is changed (in a Roo application)? Because there are several problems:
How to determine if the issue state is changed?
We need to make sure, that the message send after the issue is complete updated (for example it would not work, to put the trigger in the setState() method of the Issue, because it is not guaranteed that the other values from the form (title…) are updated before the state is changed.
The mail must only be sended if the form was valid and the Issue is likely to be saved (I do not facing the problem that the transaction cannot be committed – this will be another problem)
Does anybody have a good, testable (unit tests) and maintainable solution? Maintainable means especially that the code to handle this should not be placed in the controller, because it will be used in several controllers and someday somebody will implement an new controller and he will likely forget to handle this email concern.
You can use the #PostUpdate annotation, a JPA life cycle callback listener.
class Issue{
#PostUpdate
protected void onPostUpdate(){
//This method wil run after the update
if(this.state == Resolved){
//...
}
}
Here is more information about the available callbacks.