Struts2学习笔记(第二天)

    xiaoxiao2021-04-18  71

    1.    今日任务

    ·Struts2结果处理方式

    ·Struts2获得ServletAPI

    ·Struts2参数获得方式

    ·CRM练习案例-添加客户

     

    2.相关知识

    2.1 Struts2结果处理方式

    2.1.1全局结果页面

    <global-results>

        <result name="success">/demo3/demo2.jsp</result>

    </global-results>

     

    2.1.2 局部结果页面

    Struts2的结果类型在struts2-core-2.3.32.jar包下的struts-default.xml配置文件里

    <!-- 转发到action --> <result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/> <!-- 转发到jsp --> <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/> <result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/> <result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/> <!-- 重定向到jsp --> <result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/> <!-- 重定向到action --> <result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/> <!-- 用于下载 --> <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/> <result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/> <result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/> <result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /> <result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" />

    2.2 Struts2获得ServletAPI

    原理图:

    2.2.1 通过ActionContext

    public class Demo1Action extends ActionSupport { public String execute() throws Exception { //request域=> map (struts2并不推荐使用原生request域) //不推荐 Map<String, Object> requestScope = (Map<String, Object>) ActionContext.getContext().get("request"); //推荐 ActionContext.getContext().put("name", "requestTom"); //session域 => map Map<String, Object> sessionScope = ActionContext.getContext().getSession(); sessionScope.put("name", "sessionTom"); //application域=>map Map<String, Object> applicationScope = ActionContext.getContext().getApplication(); applicationScope.put("name", "applicationTom"); return SUCCESS; } }

    2.2.2 通过ServletActionContext

    public class Demo2Action extends ActionSupport { //并不推荐 public String execute() throws Exception { //原生request HttpServletRequest request = ServletActionContext.getRequest(); //原生session HttpSession session = request.getSession(); //原生response HttpServletResponse response = ServletActionContext.getResponse(); //原生servletContext ServletContext servletContext = ServletActionContext.getServletContext(); return SUCCESS; } }

    2.2.3 通过实现接口访问

    public class Demo7Action extends ActionSupport implements ServletRequestAware { private HttpServletRequest request; public String execute() throws Exception { System.out.println("原生request:"+request); return SUCCESS; } @Override public void setServletRequest(HttpServletRequest request) { this.request = request; } }

    2.3 Struts2参数获得方式

    2.3.1 属性驱动获得参数

    代码:

    public class Demo8Action extends ActionSupport { public Demo8Action() { super(); System.out.println("demo8Action被创建了!"); } //准备与参数键名称相同的属性 private String name; //自动类型转换 只能转换8大基本数据类型以及对应包装类 private Integer age; //支持特定类型字符串转换为Date ,例如 yyyy-MM-dd private Date birthday; public String execute() throws Exception { System.out.println("name参数值:"+name+",age参数值:"+age+",生日:"+birthday); return SUCCESS; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }

    前台页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo8Action"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> </body> </html>

    2.3.2 对象驱动

    代码:

    //struts2如何获得参数-方式2 public class Demo2Action extends ActionSupport { //准备user对象 private User user; public String execute() throws Exception { System.out.println(user); return SUCCESS; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }

    前台页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo9Action"> 用户名:<input type="text" name="user.name" /><br> 年龄:<input type="text" name="user.age" /><br> 生日:<input type="text" name="user.birthday" /><br> <input type="submit" value="提交" /> </form> </body> </html>

    2.3.3 模型驱动

    代码:

    public class Demo3Action extends ActionSupport implements ModelDriven<User> { //准备user 成员变量 private User user =new User(); public String execute() throws Exception { System.out.println(user); return SUCCESS; } @Override public User getModel() { return user; } }

    前台页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo10Action"> 用户名:<input type="text" name="name" /><br> 年龄:<input type="text" name="age" /><br> 生日:<input type="text" name="birthday" /><br> <input type="submit" value="提交" /> </form> </body> </html>

    2.3.4 集合类型参数封装

    代码:

    //struts2 封装集合类型参数 public class Demo11Action extends ActionSupport { //list private List<String> list; //Map private Map<String,String> map; public String execute() throws Exception { System.out.println("list:"+list); System.out.println("map:"+map); return SUCCESS; } public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public Map<String, String> getMap() { return map; } public void setMap(Map<String, String> map) { this.map = map; } }

    前台页面:

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/Demo11Action" method="post" > list:<input type="text" name="list" /><br> list:<input type="text" name="list[3]" /><br> map:<input type="text" name="map['haha']" /><br> <input type="submit" value="提交" /> </form> </body> </html>

    3.crm案例

    CRM练习案例-添加客户

    编写Action类save方法

    public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ private Customer customer = new Customer(); private CustomerService customerService = new CustomerServiceImpl(); /** * 获取客户列表 * @return * @throws Exception */ public String list() throws Exception{ DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); //获取请求参数(明天才学Struts2参数获取 暂时用此方法代替) HttpServletRequest request = ServletActionContext.getRequest(); //获取当期页 int currentPage = 1; //如果参数为空 默认显示第一页 if (request.getParameter("currentPage")!=null && !request.getParameter("currentPage").equals("")) { currentPage = Integer.parseInt(request.getParameter("currentPage")); } //获取筛选客户名称 String cust_name = request.getParameter("cust_name"); if (cust_name!=null) { //如果筛选客户名称不为空添加模糊查询条件 dc.add(Restrictions.like("cust_name", "%"+cust_name+"%")); } //设置每一页显示几条记录 int pageSize = 10; //调用业务层获取客户列表 PageBean<Customer> pb = customerService.getCustomerByPage(dc,currentPage,pageSize); request.setAttribute("pb", pb); return "list"; } /** * 添加客户 * @return * @throws Exception */ public String save() throws Exception{ customerService.save(customer); return "toList"; } @Override public Customer getModel() { // TODO Auto-generated method stub return customer; } }

    修改struts配置文件

    <?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> <constant name="struts.devMode" value="true"></constant> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <constant name="struts.configuration.xml.reload" value="true" /> <package name="crm" namespace="/" extends="struts-default"> <action name="CustomerAction_*" class="com.itheima.web.action.CustomerAction" method="{1}"> <result name="list">/jsp/customer/list.jsp</result> <result name="toList" type="redirectAction"> <param name="actionName">CustomerAction_list</param> <param name="namespace">/</param> </result> </action> </package> </struts>

    测试

    转载请注明原文地址: https://ju.6miu.com/read-675546.html

    最新回复(0)