主要步骤:
1. 提交页面:一个查看商品的链接
结果页面:商品列表,显示三件商品的具体信息
2. 实体类:商品名称,价格
3. 后台控制类Servlet和配置
4. 添加spring开发包
5. 实体类改进:Spring IoC配置
applicationContext.xml配置文件
结果页面 details.jsp
1. 提交页面:一个查看商品的链接
结果页面:商品列表,显示三件商品的具体信息
2. 实体类:商品名称,价格
3. 后台控制类Servlet和配置
4. 添加spring开发包
5. 实体类改进:Spring IoC配置
控制类改进:Spring IoC程序
提交页面一个查看商品的链接:
<%@ 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>
This is my JSP page. <br>
<a href="domain">查看已购买的商品</a>
</body>
</html>
注意:实体勒种必须要有无参构造函数,否则在applicationContext.xml配置文件中将会报错
No constructor with 0 arguments defined in class 'Spring_modle.Product' applicationContext.xml /Spring_Ioc_DI/src line 7 Spring Beans Problem
package Spring_modle;
public class Product {
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the pirce
*/
public double getPirce() {
return pirce;
}
/**
* @param pirce the pirce to set
*/
public void setPirce(double pirce) {
this.pirce = pirce;
}
private String name;
private double pirce;
/**
* 有参构造
* @param name
* @param pirce
*/
public Product(String name, double pirce) {
super();
this.name = name;
this.pirce = pirce;
}
/**
* @author tianjie
*Spring注入必须要有无参构造,否则 applicationContext.xml将会报错
* 无参构造
* */
public Product() {
}
}
后台控制类servlet
package Spring_Control;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Spring_modle.Product;
public class domain extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/**
* 静态获取
* */
// ApplicationContext con=
// new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
//
// Proudect p1,p2,p3;
// p1=con.getBean("p1",Proudect.class);
// p2=con.getBean("p2",Proudect.class);
// p3=con.getBean("p3",Proudect.class);
//
// List<Proudect> list=new ArrayList<Proudect>();
// list.add(p1);
// list.add(p2);
// list.add(p3);
/**
* 动态获取
* */
ApplicationContext con=
new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
String[] p=con.getBeanNamesForType(Product.class);
List<Product> list=new ArrayList<Product>();
Product proudect;
for(int i=0;i<p.length;i++){
proudect=con.getBean(p[i],Product.class);
list.add(proudect);
}
HttpSession session=request.getSession();
session.setAttribute("list", list);
response.sendRedirect("details.jsp");
}
}
applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="p1" class="Spring_modle.Product" ><!-- 控制反转 -->
<property name="name" value="化妆品"></property><!-- 依赖注入 -->
<property name="pirce" value="300"></property>
</bean>
<bean name="p2" class="Spring_modle.Product"><!-- 控制反转 -->
<property name="name" value="轮滑鞋"></property><!-- 依赖注入 -->
<property name="pirce" value="500"></property>
</bean>
<bean name="p3" class="Spring_modle.Product"><!-- 控制反转 -->
<property name="name" value="书"></property><!-- 依赖注入 -->
<property name="pirce" value="30"></property>
</bean>
</beans>
结果页面 details.jsp
<%@page import="java.util.List"%>
<%@page import="Spring_modle.Proudect"%>
<%@ 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>
<center>
<%
out.write("输出商品:<br>");
List<Proudect> list=(List<Proudect>)session.getAttribute("list");
for(int i=0;i<list.size();i++){
out.write("商品名称:"+list.get(i).getName()+"<br>");
out.write("商品名称:"+list.get(i).getPirce()+"<br>");
}
%>
</center>
</body>
</html>