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">
<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;
/**
* 流式 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();
}
}
}
}
(3)Service
略
(4)Dao
略
3、前端Js
jQuery.ajax({
url: "userActionUrl!loadUserHtml.action?id=" + 1,
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);
}
});