Structs2项目举例

Spring + Structs2 + ?

1、Xml配置

<!-- struts-web.xml -->
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="user-package" extends="base-package">
       <action name="userActionUrl" class="userActionBean">  <!-- 决定请求的URL -->
          <result name="userModule">/page/user-module.jsp</result>
       </action>
    </package>
</struts>

<!-- spring-web.xml -->
<bean id="userActionBean" class="com.action.UserAction" scope="prototype">
    <property name="userService" ref="userServiceBean"></property>  <!-- 成员变量名相同 -->
</bean>

注:Service、Dao配置略


2、后台Java

(1)基类

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class BaseAction extends ActionSupport
        implements ServletRequestAware, ServletResponseAware, ParameterAware, CookiesAware, SessionAware, ApplicationAware {

    private HttpServletRequest request;
    private HttpServletResponse reponse;

    protected Map<String, String[]> parameters;
    protected Map<String, String> cookies;
    protected Map<String, Object> session;
    protected Map<String, Object> application;

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.reponse = reponse;
    }

    public HttpServletResponse getReponse() {
        return reponse;
    }

    @Override
    public void setParameters(Map<String, String[]> parameters) {
        this.parameters = parameters;
    }

    @Override
    public void setCookiesMap(Map<String, String> cookies) {
        this.cookies = cookies;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.session = session;
    }

    @Override
    public void setApplication(Map<String, Object> application) {
        this.application = application;
    }

}

(2)Action

    public class UserAction extends BaseAction {

        @Autowired
        private UserService userService;  // 与spring>bean>property name相同

        public UserService getUserService() {  // spring mvc使用注解,不用声明get、set
            return userService;
        }

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

        /**
         * 页面跳转时先执行的默认方法
         * (1)可校验用户权限
         * (2)可对JSP界面使用到的变量赋值
         */
        public String execute() {  // structs2 特有
            //JSP中使用到了 ${id} 变量
            this.getRequest().setAttribute("id", 1);
            return SUCCESS;
        }


        /**
         * 流式 HTML
         */
        public void loadUserHtml() {
            String id = (String) this.getRequest().getParameter("id");
            String htmlJson = userService.getHtmlJson(id);
            this.toHtml(htmlJson);
        }

        /**
         * 字符 HTML
         */
        public String getHtmlString() {
            String id = (String) this.getRequest().getParameter("id");
            String htmlJson = userService.getHtmlJson(id);
            return htmlJson;
        }

        /**
         * html写入流
         */
        public void toHtml(String htmlJson) {
            PrintWriter out = null;
            try {
                HttpServletResponse response = this.getReponse();
                response.setCharacterEncoding("UTF-8");
                response.setContentType("text/html;charset=UTF-8");
                out = response.getWriter();
                out.print(htmlJson);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != out) {
                    out.close();
                }
            }
        }

    }

    注:Service、Dao方法略


3、前端Js

    jQuery.ajax({
        url: "userActionUrl!loadUserHtml.action?id=" + 1, //格式:配置Action名 + ! + Java方法名 + ?参数
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        dataType: "html",
        cache: false,
        async: false,
        success: function (html) {
            $("#body_div").empty().append(html);
        }
    });




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值