struts2注解

1.struts的动作方法可以为void,没有结果视图,在动作方法中将json数据输出到页面中。

 

public void ajaxUsers(){

  response.getWriter().print();

}

2.使用注解的jar包:

struts-convention-plugin.jar

asm.jar

asm-commons.jar

 

3.使用注解:

 

(1)@Action

 

(2)@Actions({@Action()})

 

(3)@NameSpace

 

(4)@ParentPackage

 

(5)@Result

params={"key1", "value1", "key2", "value2"})

 

   直接上代码:

 

 

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.action.extension" value="do,," />

    <!--拦截器-->
    <package name="my-default" extends="json-default" abstract="true">
        <interceptors>
            <interceptor name="loginInterceptor" class="cn.getword.interceptor.LoginInterceptor" />
            <interceptor-stack name="my_intercept_stack">
                <interceptor-ref name="loginInterceptor" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>

        <default-interceptor-ref name="my_intercept_stack" />

        <global-results>
            <result name="login" type="redirect">/login2</result>
        </global-results>
    </package>
</struts>
struts.xml

 

 

package cn.getword.action;

import cn.getword.domain.User;
import cn.getword.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
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.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@ParentPackage("my-default")
@Namespace("/user")
public class UserAction extends ActionSupport implements ModelDriven<User> {
    @Autowired
    private UserService userService;
    private User user = new User();

    private int rs;
    Map<String, Boolean> result;

    @Action(value = "add", results = {@Result(location = "/WEB-INF/views/addUser.jsp")})
    public String addUser() {
        return SUCCESS;
    }

    @Action(value = "addPro", results = {
            @Result(name = "success",type = "json", params = {"contetType","application/json", "root", "rs"})})
    public String addUserPro() {
        user.setRegisterTime(new Date());
        rs = userService.addUser(user);
        return SUCCESS;
    }

    @Action(value = "update", results = {@Result(location = "/WEB-INF/views/updateUser.jsp")})
    public String updateUser() {
        HttpServletRequest request = ServletActionContext.getRequest();
        User user1 = userService.getUserById(user.getId());
        request.setAttribute("user", user1);
        return SUCCESS;
    }

    @Action(value = "updatePro", results = {
            @Result(name = "success",type = "json", params = {"contetType","application/json", "root", "rs"})})
    public String updateUserPro() {
        rs = userService.updateUser(user);
        return SUCCESS;
    }

    @Action(value = "checkUsername", results = {
            @Result(name = "success",type = "json", params = {"contetType","application/json", "root", "result"})})
    public String checkUsername() {
        User user1 = userService.getUserByUsername(user.getUsername());
        result = new HashMap<String, Boolean>();
        if (user1 != null) {
            result.put("valid", false);
        } else {
            result.put("valid", true);
        }
        return SUCCESS;
    }

    @Action(value = "delete", results = {
            @Result(name = "success",type = "json", params = {"contetType","application/json", "root", "rs"})})
    public String deleteUser() {
        if (userService.deleteUser(user.getId())) {
            rs = 1;
        } else {
            rs = 0;
        }
        return SUCCESS;
    }

    //======================
    @Override
    public User getModel() {
        return user;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public int getRs() {
        return rs;
    }

    public void setRs(int rs) {
        this.rs = rs;
    }

    public Map<String, Boolean> getResult() {
        return result;
    }

    public void setResult(Map<String, Boolean> result) {
        this.result = result;
    }
}
UserAction.java

 

 

 

 

 

 

 

end

 

posted @ 2018-09-29 16:40  fight139  阅读(111)  评论(0编辑  收藏  举报