當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring_mvc ioc/DI 控制反转与依赖注入
生活随笔
收集整理的這篇文章主要介紹了
Spring_mvc ioc/DI 控制反转与依赖注入
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
主要步驟:
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"?> <beansxmlns="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>
總結
以上是生活随笔為你收集整理的Spring_mvc ioc/DI 控制反转与依赖注入的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 判断是否有网络连接,判断
- 下一篇: Struts2 ognl表达式