struts2使用response和request功能
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 覆盖掉Struts2一些常量的默认值 --> <constant name="struts.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="true" /> <constant name="struts.action.extension" value="action,,do"></constant> <constant name="struts.configuration.xml.reload" value="true"></constant> <constant name="struts.locale" value="zh_CN"></constant> <package name="p1" extends="struts-default"> <action name="test1" class="com.itheima.actions.ActionDemo1"></action> </package> </struts>
ActionDemo1.java:
package com.itheima.actions; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; public class ActionDemo1 implements ServletResponseAware,ServletRequestAware{ private HttpServletResponse response; private HttpServletRequest request; public String execute() throws IOException{ //方式一: // ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8"); // ServletActionContext.getResponse().getWriter().write("你好");//不是Struts不起作用,没有用Struts2进行输出 // // HttpSession session = ServletActionContext.getRequest().getSession(); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("吃了吗"); return null; } public void setServletResponse(HttpServletResponse response) { this.response = response; } public void setServletRequest(HttpServletRequest request) { this.request = request; } }