在以下代码段中,awardTypes
来自何处?相关网址:http://localhost:8080/awardTypes/edit/1
。我需要帮助理解请求流程。
/mgs/grails-app/controllers/com/mgs/AwardTypesController.groovy
class AwardTypesController {
...
def edit(AwardTypes awardTypes) {
respond awardTypes
}
...
}
/mgs/grails-app/conf/hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<mapping resource="AwardTypes.hbm.xml"/>
</session-factory>
</hibernate-configuration>
/mgs/grails-app/conf/AwardTypes.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated 14-Oct-2015 10:13:15 PM by Hibernate Tools 4.3.1.Final -->
<hibernate-mapping>
<class name="com.mgs.AwardTypes" table="award_types" catalog="mgs" optimistic-lock="version">
<id name="id" type="java.lang.Byte">
<column name="id" />
<generator class="identity" />
</id>
<property name="name" type="string">
<column name="name" length="250" not-null="true" />
</property>
<property name="image" type="string">
<column name="image" length="500" />
</property>
<property name="created" type="timestamp">
<column name="created" length="19" />
</property>
<property name="modified" type="timestamp">
<column name="modified" length="19" />
</property>
<set name="awardses" table="awards" inverse="true" lazy="true" fetch="select">
<key>
<column name="award_type_id" not-null="true" />
</key>
<one-to-many class="com.mgs.Awards" />
</set>
</class>
</hibernate-mapping>
配置:
grails generate-all com.mgs.AwardTypes
我原以为会是这样的:
class AwardTypesController {
...
def edit(int id) {
respond AwardTypes.get(id)
}
...
}
答案 0 :(得分:0)
您必须在 UrlMappings .groovy中添加此代码:
class UrlMappings {
static mappings = {
.....
"/awardTypes/edit/$id"(controller: "awardTypes", action: "edit")
}
}
在控制器 1.)中:
def edit() {
AwardTypes.get(params.id)
}
或强>
def edit(Long id) {
AwardTypes.get(id)
}
更好地使用长而不是 int ,因为在grails中它是id的默认类型。
*另外,我可以显示我的UrlMapping:
class UrlMappings {
static mappings = {
...
"/$controller/$action?/$id?(.$format)?" {
constraints {
// apply constraints here
}
}
....
}
}
另外,请阅读更多内容:https://grails.github.io/grails-doc/latest/ref/Plug-ins/URL%20mappings.html
答案 1 :(得分:0)
这完全归功于Data Binding,它由Grails自动完成。