My controller has an object as parameter
Function Search(ByVal model As ItemSearchModel) As ActionResult
Which look something like this
Public Class ItemSearchModel
Public Property SearchQuery As String
And, as you can imagine, the url will look this like
/Search?SearchQuery=test
I want to change the query string to have a small variable, sort of like
/Search?s=test
Is there a built-in way I could keep the same variable name in my class? Something like
Public Class ItemSearchModel
<QueryString(Name:="s")> _
Public Property SearchQuery As String
I think you can use the ActionParameterAlias package from Nuget to accomplish what you want.
You can define two properties, both pointing to the same field. Then you can access that item using either s or SearchQuery from the URL.
Public Class ItemSearchModel
Private _s As String
Public Property s() As String
Get
Return _s
End Get
Set(value As String)
_s = value
End Set
End Property
Public Property SearchQuery() As String
Get
Return _s
End Get
Set(value As String)
_s = value
End Set
End Property
End Class
Related
I have found an interesting (to me) place in Dart code:
factory Uri(
{String scheme,
String userInfo,
String host,
int port,
String path,
Iterable<String> pathSegments,
String query,
Map<String, dynamic /*String|Iterable<String>*/ > queryParameters,
String fragment}) = _Uri; // <==== here
and then:
class _Uri implements Uri {
...
}
It looks like the class _Uri is assigned to the factory constructor. I don't think I have read about it in the language tour or anywhere else. What is this 'technique' called? How does it work? Are there any special requirements for the factory constructor and the class for this to work?
I have created new MVC project with internet template.
Now I would like to change default required message from "Username is to required" to some text in my language.
But when I try to enter string in required attribute compiler complains with message in title.
Example:
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports System.Globalization
Public Class LogOnModel
Private userNameValue As String
<DisplayName("Banana split")> _
<Required("Text in my language")> _
Public Property UserName() As String
Get
Return userNameValue
End Get
Set(ByVal value As String)
userNameValue = value
End Set
End Property
End Class
I have also tried to put something like:
<Required(ErrorMessage="Text")
This is the correct syntax, notice the : before =
<Required(ErrorMessage:="Text in my language")>
I have textfield for birthDate. When a user enter invalid date, let say for example a String, error message successfully displayed as fielderror. But in my console, I got this error:
java.lang.NoSuchMethodException: Profile.setBirthDate([Ljava.lang.String;)
Have I missed something that's why I encountered the error?
In your Action class you dont have a method called setBirthDate(String birthDate), add it your issue should be resolved.
Note check to see that you have placed all getter and setter in your action class for all properties.
I think in your JSP you have :
<s:textfield name="birthDate" />
Struts will try to map this to setBirthDate(String string), since this method is missing in your action hence the NoSuchMethodException
Update:
To convert String to Date:
public class MyStringToDateConverter extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values, Class toClass) {
//Parse String to get a date object
}
public String convertToString(Map context, Object o) {
// Get the string from object o
}
}
If you are using Annotation in your action class then add #Conversion() to your action
#Conversion()
public class MyAction extends ActionSupport{
public Date myDate = null;
#TypeConversion(converter="MyStringToDateConverter") //Fully qualified name so if this class is in mypackage then converter will be "myPackage.MyStringToDateConverter"
public void setMyDate(Date date) {
this.myDate = date;
}
}
If you dont want to use Annotation then you can look at the official documentation for example.
suppose I have MyClass domain class:
class MyClass {
String prop1
String prop2
String prop3
}
I wonder is there any way to delete for example prop1 property from MyClass object ?
The only way to actually delete the property is to remove it from the source file. However, you can make attempts to access the property exhibit the same behaviour as an attempt to access a non-existent property.
class MyClass {
String prop1
String prop2
String prop3
}
MyClass.metaClass {
// Intercept attempts to get the property
getProp1 = {-> throw new MissingPropertyException("can't get prop1")}
// Intercept attempts to set the property
setProp1 = {throw new MissingPropertyException("can't set prop1")}
}
I have a domain class containing a couple of fields. I can access them from my .gsps. I want to add a method to the domain class, which I can call from the .gsps (this method is a kind of virtual field; it's data is not coming directly from the database).
How do I add the method and how can I then call it from the .gsps?
To add a method, just write it out like you would any other regular method. It will be available on the object when you display it in your GSP.
def someMethod() {
return "Hello."
}
Then in your GSP.
${myObject.someMethod()}
If you want your method to appear to be more like a property, then make your method a getter method. A method called getFullName(), can be accessed like a property as ${person.fullName}. Note the lack of parentheses.
Consider class like below
class Job {
String jobTitle
String jobType
String jobLocation
String state
static constraints = {
jobTitle nullable : false,size: 0..200
jobType nullable : false,size: 0..200
jobLocation nullable : false,size: 0..200
state nullable : false
}
def jsonMap () {
[
'jobTitle':"some job title",
'jobType':"some jobType",
'jobLocation':"some location",
'state':"some state"
]
}
}
You can use that jsonMap wherever you want. In gsp too like ${jobObject.jsonMap()}