struts2学习之4-struts2注解
struts2学习之四-struts2注解
struts2学习之一: http://alph0618.iteye.com/blog/2162974
struts2学习之二-json插件:http://alph0618.iteye.com/blog/2162989
struts2学习之三-spring注解:http://alph0618.iteye.com/admin/blogs/2162996
在build.gradle需要添加"org.apache.struts:struts2-convention-plugin:2.3.16.3"
1、把在struts.xml配置的action注释掉;
2、在UserJsonAction类中配置注解:@Action、@ParentPackage、@Namespace,由于不想name属性出现在返回json数据中,所以在name的get方法是配置@JSON(serialize = false):
3、发布启动服务,访问http://localhost:8080/struts2demo/a/helloworld.action。
struts2学习之一: http://alph0618.iteye.com/blog/2162974
struts2学习之二-json插件:http://alph0618.iteye.com/blog/2162989
struts2学习之三-spring注解:http://alph0618.iteye.com/admin/blogs/2162996
在build.gradle需要添加"org.apache.struts:struts2-convention-plugin:2.3.16.3"
1、把在struts.xml配置的action注释掉;
2、在UserJsonAction类中配置注解:@Action、@ParentPackage、@Namespace,由于不想name属性出现在返回json数据中,所以在name的get方法是配置@JSON(serialize = false):
package com.haochen.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.ParentPackage; import org.apache.struts2.json.annotations.JSON; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.haochen.service.IUserService; @Action(value = "userJson") @ParentPackage("test") @Namespace("/a") public class UserJsonAction extends BaseJsonAction { /** * */ private static final long serialVersionUID = -5818584581746655517L; private String name; // 指定bean名 @Qualifier("userService") @Autowired private IUserService userService; @Override public String execute() throws Exception { String n = name; this.success(userService.getUser(n)); /** * 1、返回值为JSON,配置文件struts.xml中的<action></action>不用设置<result * type="json"></result>。 * 2、返回值为SUCCESS,配置文件struts.xml中的<action></action>需要设置<result * type="json"></result>。 **/ return JSON; } // 不在json返回值中出现 @JSON(serialize = false) public String getName() { return name; } public void setName(String name) { this.name = name; } }
3、发布启动服务,访问http://localhost:8080/struts2demo/a/helloworld.action。