一、HelloWorld的实现
基于Struts2方式与自实现Filter方式做MVC控制器的区别在于:
需要搭建Struts2开发环境;不需要显式定义Filter,而使用的是Struts2的配置文件来实现;details.jsp更为简单,即由${requestScope.product.productName}--> ${productName},此处是依靠Struts2的值栈来实现的;Product类可以不需要创建带参的构造器,依靠params拦截器来实现请求参数名与实体类getter()与setter()方法的映射;
实现步骤: (1). 由product-input.action转到/WEB-INF/pages/input.jsp; (2). 由input.jsp页面的product-save.action到Product类的save()方法,再到/WEB-INF/pages/details.jsp; (3). 在Product类中定义save()方法,且返回值为details。
配置文件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 name="struts2-helloworld" extends="struts-default">
<action name="product-input">
<result>/WEB-INF/pages/input.jsp
</result>
</action>
<action name="product-save" class="com.qiaobc.struts2.domain.Product" method="save">
<result name="details">/WEB-INF/pages/details.jsp
</result>
</action>
</package>
</struts>
二、解决输入中文后页面上显示乱码的问题?
需要在product-input.action请求前,配置拦截器,来设置request的编码方式。
附录:具体实现代码
代码结构:
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">Product Input...
</a>
</body>
</html>
<%@ 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">
<table>
<tr>请添加商品信息:
</tr>
<tr>
<td>ProductName:
</td>
<td><input type="text" name="productName"/></td>
</tr>
<tr>
<td>ProductDesc:
</td>
<td><input type="text" name="productDesc"/></td>
</tr>
<tr>
<td>ProductPrice:
</td>
<td><input type="text" name="productPrice"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
</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>Insert title here
</title>
</head>
<body>
The Product has been saved...
<a href="index.jsp">Return
</a>
<table>
<tr>
<td>ProductID:
</td> <td>${productId }
</td>
</tr>
<tr>
<td>ProductName:
</td> <td>${productName }
</td>
</tr>
<tr>
<td>ProductDesc:
</td> <td>${productDesc }
</td>
</tr>
<tr>
<td>ProductPrice:
</td> <td>${productPrice }
</td>
</tr>
</table>
</body>
</html>
Product.java:
public class Product {
private Integer productId;
private String productName;
private String productDesc;
private double productPrice;
public Integer
getProductId() {
return productId;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
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 double getProductPrice() {
return productPrice;
}
public void setProductPrice(
double productPrice) {
this.productPrice = productPrice;
}
@Override
public String
toString() {
return "Product [productId=" + productId +
", productName="
+ productName +
", productDesc=" + productDesc
+
", productPrice=" + productPrice +
"]";
}
public String
save() {
System.
out.println(
"save:" +
this);
return "details";
}
}
转载请注明原文地址: https://ju.6miu.com/read-350220.html