·Struts2结果处理方式
·Struts2获得ServletAPI
·Struts2参数获得方式
·CRM练习案例-添加客户
<global-results>
<result name="success">/demo3/demo2.jsp</result>
</global-results>
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" />
原理图:
代码:
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>代码:
//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>代码:
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>代码:
//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>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>测试