2019独角兽企业重金招聘Python工程师标准>>>
struts2 2.5版本(一)
struts2 2.5版本(二)
struts2 2.5版本(三)
结果跳转方式
/hello.jsp
/hello.jsp
Demo
/
Demo
/
获得servletAPI
ActionContext
//session域
Map session = ActionContext.getContext().getSession();
session.put("name", "session");
//application域
Map application = ActionContext.getContext().getApplication();
application.put("name", "application");
//request域
//Map request = (Map) ActionContext.getContext().get("request");
ActionContext.getContext().put("name", "request");
ServletActionContext
HttpServletRequest request = ServletActionContext.getRequest();
request.getSession();
ServletActionContext.getResponse();
ServletActionContext.getServletContext();
实现接口方式
package com.company.api;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class APIDemo3 extends ActionSupport implements ServletRequestAware, ServletResponseAware {
private HttpServletRequest request;
private HttpServletResponse httpServletResponse;
@Override
public String execute() throws Exception {
return SUCCESS;
}
@Override
public void setServletRequest(HttpServletRequest httpServletRequest) {
this.request = request;
}
@Override
public void setServletResponse(HttpServletResponse httpServletResponse) {
this.httpServletResponse = httpServletResponse;
}
}
参数封装
属性驱动
Parm
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Param extends ActionSupport {
//与参数键名称相同的属性
private String name;
private Date birthday;
private Integer age;
@Override
public String execute() throws Exception {
System.out.println("name:" + name);
System.out.println("birthday:" + birthday);
System.out.println("age:" + age);
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;
}
}
struts.xml
/form.jsp
form.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
对象驱动
Param2
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
public class Param2 extends ActionSupport {
private User user;
@Override
public String execute() throws Exception {
System.out.printf(user.toString());
return SUCCESS;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
struts.xml
/form2.jsp
form2.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
模型驱动
Param3
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class Param3 extends ActionSupport implements ModelDriven {
private User user = new User();
@Override
public String execute() throws Exception {
System.out.printf(user.toString());
return SUCCESS;
}
@Override
public User getModel() {
return user;
}
}
struts.xml
/form3.jsp
form3.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
集合类型封装
Param4
package com.company.param;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Param4 extends ActionSupport {
private List list;
private Map map;
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
@Override
public String execute() throws Exception {
System.out.println("list:" + list);
System.out.println("map:" + map);
return SUCCESS;
}
}
struts.xml
/form4.jsp
form4.jsp
<%--
Created by IntelliJ IDEA.
User: mac
Date: 2019/3/9
Time: 5:27 PM
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title