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><!--override Struts2 Some constants default value-- <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{//Mode one://servletactioncontext.getresponse (). setContentType ("Text/html;charset=utf-8");// Servletactioncontext.getresponse (). Getwriter (). Write ("Hello");//Not struts does not work, does not use STRUTS2 for output////httpsession session = Servletactioncontext.getrequest (). getsession (); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Write ("Eat"); return null;} public void Setservletresponse (HttpServletResponse response) {this.response = response;} public void Setservletrequest (HttpServletRequest request) {this.request = Request;}}
Struts2 using response and request features