一、Struts Hello Struts
1、导入lib包
2、web.xml中添加Filter,所有请求都交给这个Filter
<web-app>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3、src目录下,创建struts.xml配置文件(访问index路径时,跳转到index.jsp)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="basicstruts" extends="struts-default">
<action name="index">
<result>index.jsp</result>
</action>
</package>
</struts>
4、创建index.jsp
Hello Struts2 World
5、tomcat中启动,访问地址http://127.0.0.1:8080/struts/index
6、思路:
- 所有访问,都被web.xml中配置的Struts的Filter拦截
- 拦截后,进入stuts的工作流程
- 访问地址index,根据struts.xml中的配置,服务端跳转到index.jsp
三、Struts 显示数据到JSP
1、创建Product.java模型,存放数据
package com.how2java.bean;
public class Product {
int id;
String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2、创建ProductAction用户控制
package com.how2java.action;
import com.how2java.bean.Product;
public class ProductAction {
private Product product;
public String show() {
product = new Product();
product.setName("iphone7");
return "show";
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
}
3、在struts.xml中配置跳转
访问“showProduct” 会调用ProductAction类的show方法,服务端返回到show.jsp
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="basicstruts" extends="struts-default">
<action name="showProduct" class="com.how2java.action.ProductAction" method="show">
<result name="show">show.jsp</result>
</action>
</package>
</struts>
4、show.jsp
<%@page isELIgnored="false"%>
${product.name}
Struts通过getProduct()方法返回product对象,再把product对象,通过request.setAttribute("product",product)放在“product”这个key上,这样就方便EL表达式取出
5、测试,访问地址 http://localhost:8080/showProduct
6、流程
-
访问路径/showProduct
-
所有访问都被struts的Filter拦截,进入struts的工作流程
-
根据struts.xml配置,执行ProductAction的show方法
-
show方法中,将实例属性指向新的product对象,设置name为iphone7
-
服务端跳转到show.jsp
-
show.jsp中,访问ProductAction.getProduct()获取实例属性product,并显示名称iphone7
7、原理图
四、Struts提交数据到Action
1、提交数据页面addProduct.jsp
<html>
<head>
<title>提交产品</title>
</head>
<body>
<form action="addProduct">
<input type="text" name="product.name">
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
2、配置struts.xml
<action name="addProduct" class="com.web.action.ProductAction" method="add">
<result name="show">show.jsp</result>
</action>
3、Action中增加add方法
public String add(){
System.out.println("product.name:"+product.getName());
return "show";
}
在addProduct.jsp中提交数据的field是product.name
会自动调用对应的Action的setProduct(Product product)方法进行数据的注入
所以ProductAction必须提供setProduct(Product product)方法
在执行到第2行的时候,jsp传过来的name已经被注入进了属性product中了
4、测试
http://localhost:8080/addProduct.jsp
5、原理图
五、Struts的中文问题
Struts的中文问题,由3部分组成
1. jsp提交数据的时候,必须是UTF-8编码的
2. struts拿到数据后进行UTF-8解码
3. 服务端跳转到jsp进行显示的时候,要指定浏览器使用UTF-8进行显示
UTF-8可以换成GBK或者GB2312,但是必须统一,不能混用
1、提交数据的addProduct.jsp
提交数据的时候指定编码方式UTF-8
并且设置form 的method为post方式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<html>
<form action="addProduct" method="post">
<input type="text" name="product.name">
<br/>
<input type="submit" value="submit">
</form>
</html>
2、struts.xml指定解码方式为utf-8
<struts> <constant name="struts.i18n.encoding" value="UTF-8"></constant>
3、show.jsp指定浏览器以utf-8显示
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
补充:
1、post提交方式
解析请求参数前加一句如下的代码即可:
request.setCharacterEncoding("utf-8");
接受参数时进行转码,例如:
String s=new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312") ;
或者在web.xml中,配置servlet规范中的过滤器
<filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SetCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、get提交方式
原因也是tomcat的内部编码格式iso8859-1导致。Tomcat会以get的缺省编码方式iso8859-1对汉字进行编码,编码后追加到url,导致接受页面得到的参数为乱码。解决:
a.使用POST提交方式解决办法的第一种方式,对接受到的字符进行解码,再转码
使用String进行 转码时,往往都是先从 ISO-8859-1 格式的字符串中取出字节内容,然后再用页面相应的编码格式重新构造一个新的字符串,像本示例:
(new String(country.getBytes(“ISO-8859-1”), “utf-8”))
b.tomcat下server.xml的Connector节点增加useBodyEncodingForURI="true"属性配置,然后在JSP页面中加入<%request.seCharacterEncoding("gb2312");%>所设置的编码格式进行编码。
六、Struts 获取request和response对象
action中:
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
七、获取SESSION对象
struts中的Session有两个
一个是传统的servlet包下的HttpSession
另一个是Struts中自己定义的Session
传统的servlet包下的session的获取办法是:
ServletActionContext.getRequest().getSession();
使用该方法,需要在eclipse的项目中导入servlet-api.jar,可以在右边下载
新的Session的获取办法是
Map m = ActionContext.getContext().getSession();
这个session以Map类的形式出现,其中的值和HttpSession中的值是同步的
八、上传文件
1、upload.jsp中,from表单配置
上传一定要为form加上enctype="multipart/form-data",表示提交的数据是二进制的
并且必须是method="post"
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
上传文件 : <input type="file" name="doc" /> <br>
<input type="submit" value="上传">
</form>
</body>
</html>
2、UploadAction
在upload.jsp中file字段对应的name是"doc"
所以在action中,必须准备3个属性,分别是
File doc;
String docFileName;
String docContentType;
属性名字不能使用其他的,必须基于“doc"
然后为这3个属性提供getter setter
3、sturts.xml增加action配置
<action name="upload" class="com.web.action.UploadAction" method="upload">
<result name="success">success.jsp</result>
</action>
4、跳转的视图success.jsp
<%@page isELIgnored="false"%>
uploaded success
${doc}
<br/>
${docFileName}
<br/>
${docContentType}
<br/>
5、上传文件最大限制
struts.xml中增加常量配置
<struts>
<constant name="struts.multipart.maxSize" value="10240000"/>
</struts>