Struts2第一个程序
2013-09-02 00:05
411 查看
Struts2的优点:
在体系结构方面更优秀:
类更少, 更高效: 在 Struts2 中无需使用 “ActionForm” 来封装请求参数.
扩展更容易: Struts2 通过拦截器完成了框架的大部分工作. 在 Struts2 中插入一个拦截器对象相当简便易行.
更容易测试:即使不使用浏览器也可以对基于 Struts2 的应用进行测试
搭建 Struts2 的环境:
Web.xml:
struts.xml
ProductAction
product_input.jsp
product_Details.jsp
运行结果:
在体系结构方面更优秀:
类更少, 更高效: 在 Struts2 中无需使用 “ActionForm” 来封装请求参数.
扩展更容易: Struts2 通过拦截器完成了框架的大部分工作. 在 Struts2 中插入一个拦截器对象相当简便易行.
更容易测试:即使不使用浏览器也可以对基于 Struts2 的应用进行测试
搭建 Struts2 的环境:
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置 struts2 的 StrutsPrepareAndExecuteFilter, 拦截所有请求--> <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>
struts.xml
<?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> <!-- 要使用struts2的功能,需要继承 struts-default包--> <package name="helloStruts2" extends="struts-default"> <!-- action:代表一个请求 name:和servletPath对应,即请求的action名称 --> <action name="Input_Product"> <result>/WEB-INF/Product/product_input.jsp</result> </action> <action name="Product_save" class="com.xxc.test1.ProductAction"> <result name="success">/WEB-INF/Product/product_Details.jsp</result> </action> </package> </struts>
ProductAction
public class ProductAction { private String productName; private int productPrice; //struts2默认会执行这个方法 public String execute(){ return "success"; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public int getProductPrice() { return productPrice; } public void setProductPrice(int productPrice) { this.productPrice = productPrice; } }
product_input.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="Product_save.action"> <table> <tr> <td>ProductName: </td> <td><input type="text" name="productName"/></td> </tr> <tr> <td>ProductPrice: </td> <td><input type="text" name="productPrice"/></td> </tr> <tr> <td colspan="2"> <input type="submit" value="Submit"/> </td> </tr> </table> </form> </body> </html>
product_Details.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Insert title here</title> </head> <body> <h4>Product Details</h4> productName: ${requestScope.productName }<br><br> productPrice: ${requestScope.productPrice } <br><br> <!-- 发现这个request已经是被struts2包装过的了 --> request: <%= request %> </body> </html>
运行结果:
相关文章推荐
- struts2环境搭建和第一个程序
- Struts2教程1:第一个Struts2程序
- [置顶] 第一个struts2程序
- 配置第一个Struts2的简单登陆程序
- Struts2教程1:第一个Struts2程序
- 如何创建struts2第一个程序及其原理解释
- Struts2学习总结(二):第一个Struts2程序
- struts2 学习笔记一 第一个struts2程序
- 第一个Struts2程序
- struts2第一个程序
- 第一个Struts2程序
- 【Struts2】(1)第一个程序
- Struts2教程1:第一个Struts2程序
- mac下 Struts2 第一个程序的详细步骤(附带源码链接)
- struts2总结二:第一个简单的struts2程序
- 第一个struts2程序的配置
- Struts2教程1:第一个Struts2程序
- 第一个 Struts2 程序
- struts2(第一个小程序)
- 第一个struts2程序