然后编写struts.xml文件,并放在WebRoot/WEB-INF/lib目录下:如下代码: View Code <?xml version="1.0" encoding="UTF-8"?> "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> 配置好后,我们再根据标签内容来编写action。方法为method对应的login,类名为loginAction,?注意:包继承为:json-default ,输出结果类型为json如下: View Code public class loginAction extends ActionSupport implements ServletRequestAware,ServletResponseAware { /** * */ private static final long serialVersionUID = 1L; HttpServletRequest request; HttpServletResponse response; public void setServletRequest(HttpServletRequest request) { this.request=request; } public void setServletResponse(HttpServletResponse response) { this.response=response; } public void login(){ try { //HttpServletRequest request =ServletActionContext.getRequest(); // HttpServletResponse response=ServletActionContext.getResponse(); this.response.setContentType("text/html;charset=utf-8"); this.response.setCharacterEncoding("UTF-8"); if(this.request.getParameter("username").equals("123456")){ this.response.getWriter().write("真的很奇怪,日本人!"); }else if(this.request.getParameter("username").equals("zhd")){ this.response.getWriter().write("没有错,我就是东子哥!"); }else{ this.response.getWriter().write("我就是东子哥!"); } //将要返回的实体对象进行json处理 // JSONObject json=JSONObject.fromObject(this.getUsername()); //输出格式如:{"id":1, "username":"zhangsan", "pwd":"123"} // System.out.println(json); // this.response.getWriter().write(json.toString()); /** JSONObject json=new JSONObject(); json.put("login", "login"); response.setContentType("text/html;charset=utf-8"); System.out.println(json); byte[] jsonBytes = json.toString().getBytes("utf-8"); response.setContentLength(jsonBytes.length); response.getOutputStream().write(jsonBytes); **/ /** JSONObject json=new JSONObject(); json.put("login", "login"); byte[] jsonBytes = json.toString().getBytes("utf-8"); response.setContentType("text/html;charset=utf-8"); response.setContentLength(jsonBytes.length); response.getOutputStream().write(jsonBytes); response.getOutputStream().flush(); response.getOutputStream().close(); **/ } catch (Exception e) { e.printStackTrace(); } // return null; }}
运行查看下: 当然你可以输入其他参数的URL