比较笨,看了三遍才能理解敲对并正确运行:
step:
1、建立web工程( Dynamic Web project)一定要勾上创建web.xml
2、导入jar包
这个就比较坑了,我查了有半个小时才查出来,我为啥一运行就报错404找不到资源(后来发现是因为WEB-INF/lib下没有引入依赖jar)
jar分两个位置引入:
1)、正常位置引入lib:
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjweaver-1.7.1.jar
commons-fileupload-1.2.2.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
druid-0.2.8.jar
hamcrest-core-1.3.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.7.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
jackson-annotations-2.1.0.jar
jackson-core-2.1.0.jar
jackson-core-asl-1.9.11.jar
jackson-databind-2.1.0.jar
jackson-mapper-asl-1.9.11.jar
javassist-3.15.0-GA.jar
javax.servlet-api-3.0.1.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
junit-4.12.jar
log4j-1.2.17.jar
MysqL-connector-java-5.1.21.jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
2)、在WEB-INF/lib下加入jar
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
3、创建实体Hellow
package com.strus2.entity; public class Hellow { private String str; void setStr(String str) { this.str = str; } hellow(){ System.out.println("hellow,"+str); } }
创建bean文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> bean id="hellow" class="com.strus2.entity.Hellow"> property name="str" value="world!!"></property</bean> beans>
4、创建监听类
com.strus2.listener; import javax.servlet.ServletContext; javax.servlet.ServletContextEvent; javax.servlet.ServletContextListener; javax.servlet.annotation.WebListener; org.springframework.context.ApplicationContext; org.springframework.context.support.ClassPathXmlApplicationContext; /** * Application Lifecycle Listener implementation class Struslistener * */ @WebListener class Struslistener implements ServletContextListener { * Default constructor. */ public Struslistener() { // TODO Auto-generated constructor stub } * @see ServletContextListener#contextInitialized(ServletContextEvent) contextInitialized(ServletContextEvent arg0) { TODO Auto-generated method stub 获取Spring配置文件名称 ServletContext servletContext = arg0.getServletContext(); String config = servletContext.getInitParameter("configLocation"); 闯将ioc容器,将ioc容器放到ServletContext属性中 ApplicationContext ctx=new ClassPathXmlApplicationContext(config); servletContext.setAttribute("ApplicationContext",ctx); } ServletContextListener#contextDestroyed(ServletContextEvent) contextDestroyed(ServletContextEvent arg0) { TODO Auto-generated method stub } }
5、创建测试Servlet
只勾选doget复选框
com.strus2.test; java.io.IOException; javax.servlet.ServletException; javax.servlet.annotation.WebServlet; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; org.springframework.context.ApplicationContext; com.strus2.entity.Hellow; * Servlet implementation class TestServlet @WebServlet("/TestServlet") class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; HttpServlet#doGet(HttpServletRequest request,HttpServletResponse response) protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException { 从Servletcontent域对象中,获取ioc容器 ServletContext servletContext = getServletContext(); ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext" 获取所需要的bean Hellow hellow = ctx.getBean(Hellow.); hellow.hellow(); response.getWriter().append("Served at: ").append(request.getContextPath()); } }
创建inde.jsp
<%@ page language="java contentTypetext/html; charset=ISO-8859-1 pageEncodingISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> htmlheadMeta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"title>Insert title herebodya href="TestServlet">TestServleta>
6、web.xml
web-app xmlns:xsi 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_3_0.xsd" id="WebApp_ID" version="3.0"> display-name>strus2-demowelcome-file-listwelcome-file>index.html>index.htm>index.jsp>default.html>default.htm>default.jspcontext-paramparam-name>configLocationparam-value>classpath:bean.xml<!-- <listener> <listener-class>com.strus2.listener.Struslistener</listener-class> </listener> --> web-app>
注意:运行测试,需要选中整个工程,run-onserver-选择Tomcat7.0 server,即可
效果如下: