尚硅谷公开课--struts2--2--搭建struts2环境以及struts2简单例子
2015-08-07 00:00
731 查看
一、搭建struts环境
1、在eclipse中新建一个java web项目
2、复制jar包
在下载的struts2中,有一个apps文件夹,这个文件夹下的.war文件即是官方给出的例子,其中struts2-blank.war是一个空的应用,即里面什么都没有。但是这个并不是最小的应该。解压struts2-blank.war,将struts2-blank\WEB-INF\lib下的.jar文件复制到java web项目中的lib文件夹中、
3、配置web.xml文件
复制struts2-blank中web.xml中关于fileter的配置代码到java web项目的web.xml中,我的web.xml文件如下:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>struts2-2</display-name> <!-- 配置struts2的Filter --> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>这个配置的意思是:
所有的请求都要被StrutsPrepareAndExecuteFilter所拦截
4、添加struts-2的配置文件
将struts2-blank\WEB-INF\classes目录下的struts2.xml文件复制到src下。可以删除多余的东西,只保留struts根结点5、添加struts.xml的提示
复制struts2.xml中的:http://struts.apache.org/dtds/struts-2.3.dtdwindows->preferences->xml->xml catalog
点击add,将其复制到key后面的文本框中,key type选择URI,
点击file system,添加struts-2.3.dtd,位于:struts-2.3.16.3\src\core\src\main\resources
如图所示:
ok
将struts.xml重新打开,可见提示:
二、struts例子
文件列表
├─src │ │ struts.xml │ │ │ └─com │ └─laolang │ └─domain │ Product.java │ └─WebContent │ index.jsp │ │ ├─pages │ details.jsp │ input.jsp │ PS D:\program\java\tomcat\tomcat7\webapps\guigu\struts2\struts2-2>
说明:
Product.javajavaBean
index.jsp
首页,提供一个到input.jsp的链接
input.jsp
输入
details.jsp
显示
代码:
Product.java
package com.laolang.domain; /** * The Class Product. */ public class Product { /** * Instantiates a new product. */ public Product() { super(); } /** * Instantiates a new product. * * @param productId * the product id * @param productName * the product name * @param productDesc * the product desc * @param productPrice * the product price */ public Product(Integer productId, String productName, String productDesc, double productPrice) { super(); this.productId = productId; this.productName = productName; this.productDesc = productDesc; this.productPrice = productPrice; } /** * Save. * input的响应action * * @return the string */ public String save() { return "details"; } /* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { return "Product [productId=" + productId + ", productName=" + productName + ", productDesc=" + productDesc + ", productPrice=" + productPrice + "]"; } /** * Gets the product id. * * @return the product id */ public Integer getProductId() { return productId; } /** * Sets the product id. * * @param productId * the new product id */ public void setProductId(Integer productId) { this.productId = productId; } /** * Gets the product name. * * @return the product name */ public String getProductName() { return productName; } /** * Sets the product name. * * @param productName * the new product name */ public void setProductName(String productName) { this.productName = productName; } /** * Gets the product desc. * * @return the product desc */ public String getProductDesc() { return productDesc; } /** * Sets the product desc. * * @param productDesc * the new product desc */ public void setProductDesc(String productDesc) { this.productDesc = productDesc; } /** * Gets the product price. * * @return the product price */ public double getProductPrice() { return productPrice; } /** * Sets the product price. * * @param productPrice * the new product price */ public void setProductPrice(double productPrice) { this.productPrice = productPrice; } /** The product id. */ private Integer productId; /** The product name. */ private String productName; /** The product desc. */ private String productDesc; /** The product price. */ private double productPrice; }
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>struts2例子</title> </head> <body> <a href="product-input.action">Product Input</a> <br><br> </body> </html>
input.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>输入</title> </head> <body> <form action="product-save.action" method="post"> ProductId: <input type="text" name="productId"/> <br><br> ProductName: <input type="text" name="productName"/> <br><br> ProductDesc: <input type="text" name="productDesc"/> <br><br> ProductPrice: <input type="text" name="productPrice" /> <br><br> <input type="submit" value="Submit"/> <br><br> </form> </body> </html>
details.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>显示</title> </head> <body> <% request.setCharacterEncoding("UTF-8"); %> ProductId: ${productId } <br><br> ProductName: ${productName } <br><br> ProductDesc: ${productDesc } <br><br> ProductPrice: ${productPrice } <br><br> </body> </html>
struts2.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 name="demo" extends="struts-default"> <action name="product-input"> <result>/pages/input.jsp</result> </action> <action name="product-save" class="com.laolang.domain.Product" method="save"> <result name="details">/pages/details.jsp</result> </action> </package> </struts>
运行效果:
struts2.xml解释
package: 包. struts2 使用 package 来组织模块.name 属性: 必须. 用于其它的包应用当前包.
extends: 当前包继承哪个包, 继承的, 即可以继承其中的所有的配置. 通常情况下继承 struts-default
struts-default 这个包在 struts-default.xml 文件中定义.
action: 一个 struts2 的请求就是一个 action
name: 对应一个 struts2 的请求的名字(或对一个 servletPath, 但去除 / 和扩展名), 不包含扩展名
class 的默认值为: com.opensymphony.xwork2.ActionSupport
method 的默认值为: execute
result: 结果.
相关文章推荐
- java序列化与反序列化
- a[href*=javablackbelt]{color:red};
- Eclipse搭建maven开发环境
- java的native关键字
- OpenJDK 和 JDK区别
- Spring Web MVC的介绍
- spring 框架
- struts 框架
- 关于jdk的报错问题 It is indirectly referenced from required .class files
- Spring的AOP小例子,以及遇到的相关ADVICE代码不起作用为题
- Java Persistence with MyBatis 3(中文版) 第四章 使用注解配置SQL映射器
- 关于JAVA Entity entity = entityClass.getAnnotation(Entity.class);
- Hibernate工作原理及为什么要用?
- java生成饼图svg
- JAVA学习笔记(三)
- 深入java单例模式
- java代码--数3退1
- LeetCode Pascal's Triangle 杨辉三角 Java
- Eclipse--java.lang.OutOfMemoryError: PermGen space
- Eclipse中10个最有用的快捷键组合