Struts2可以提供了Web应用程序开发过程中的一些常见问题的解决方案
–对来自用户的输入数据进行合法性验证
–统一的布局
–可扩展性
–国际化和本地化
–支持Ajax
–表单的重复提交
–文件的上传下载
Struts2使用了一个过滤器作为控制器
Struts2,HTML表单将直接映射到一个POJO
Struts2中验证逻辑编写在Action中
Struts2任何一个POJO都可以作为一个Action类
Struts2在页面里使用OGNL来显示各种对象模型。
搭建Struts2的环境
1.加入jar包
2.web.xml文件中配置Struts2
3.在当前web应用的classpath下添加Struts2的配置文件
details.xml
<!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>
productId:š${productId}
productName:${productName}
productDesc:š${productDesc}
productPrice:${productPrice}
</body>
</html>
input.xml
<%@ 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="product-save.action"method="post"></form>
produceName:<input type="text" name="produceName">
produceDesc:<input type="text" name="produceDesc">
producePrice:<input type="text" name="producePrice">
<input type="submit" value="提交">
</body>
</html>
index.jsp
<%@ 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>
<a href="product-input.action">input product</a>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
product.java
package com.guigu.struts.helloworld;
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private String productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductDesc() {
return productDesc;
}
public void setProductDesc(String productDesc) {
this.productDesc = productDesc;
}
public String getProductPrice() {
return productPrice;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
public Integer getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public Product() {
super();
// TODO Auto-generated constructor stub
}
public String save() {
System.out.println(this);
return"details";
}
}
注:
result type 表示结果的类型,默认是 dispatcher(转发)
redirect(重定向)
<action class=.....ActionSupport>
默认ActionSupport
struts.xml
<?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>
<!-- package:包,struts2使用package来组织模块
name属性:必须,用于其他的包应用当前包
extends:当前包继承哪个包,继承的,即可以继承所有的配置,通常情况下继承struts-default
-->
<package name="helloworld" extends="struts-default">
<!-- 配置一个action,一个struts2的请求就是一个action -->
<action name="product-input">
<!-- name:对应一个struts2的请求名,或者对应servletpath去除action
result是一个页面 -->
<result>/WEB-INF/pages/input.jsp</result>
</action>
<action name="product-save" class="com.guigu.struts.helloworld" method="save">
<!-- name:对应一个struts2的请求名,或者对应servletpath去除action
result是一个页面 -->
<result name="details">/WEB-INF/pages/details.jsp</result>
</action>
</package>
</struts>