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配置略
(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);
}
});