spring整合strus2的Hellowworld

來源:互聯網
上載者:User

標籤:map   todo   jpa   expr   java   serial   nat   eve   hello   

比較笨,看了三遍才能理解敲對並正確運行:

step:

1、建立web工程( Dynamic Web project)一定要勾上建立web.xml

2、匯入jar包

這個就比較坑了,我查了有半個小時才查出來,我為啥一運行就報錯404找不到資源

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;    public void setStr(String str) {        this.str = str;    }    public void 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、建立監聽類

package com.strus2.listener;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Application Lifecycle Listener implementation class Struslistener * */@WebListenerpublic class Struslistener implements ServletContextListener {    /**     * Default constructor.      */    public Struslistener() {        // TODO Auto-generated constructor stub    }    /**     * @see ServletContextListener#contextInitialized(ServletContextEvent)     */    public void 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);    }    /**     * @see ServletContextListener#contextDestroyed(ServletContextEvent)     */    public void contextDestroyed(ServletContextEvent arg0)  {          // TODO Auto-generated method stub    }    }

5、建立測試Servlet

只勾選doget複選框

package com.strus2.test;import java.io.IOException;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.context.ApplicationContext;import com.strus2.entity.Hellow;/** * Servlet implementation class TestServlet */@WebServlet("/TestServlet")public class TestServlet extends HttpServlet {    private static final long serialVersionUID = 1L;    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub//        從Servletcontent域對象中,擷取ioc容器        ServletContext servletContext = getServletContext();        ApplicationContext ctx = (ApplicationContext) servletContext.getAttribute("ApplicationContext");//        擷取所需要的bean        Hellow hellow = ctx.getBean(Hellow.class);        hellow.hellow();        response.getWriter().append("Served at: ").append(request.getContextPath());    }}

建立inde.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"    pageEncoding="ISO-8859-1"%><!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=ISO-8859-1"><title>Insert title here</title></head><body>    <a href="TestServlet">TestServlet</a></body></html>

6、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_3_0.xsd" id="WebApp_ID" version="3.0">  <display-name>strus2-demo</display-name>  <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>  <context-param>    <param-name>configLocation</param-name>    <param-value>classpath:bean.xml</param-value>  </context-param><!--   <listener>    <listener-class>com.strus2.listener.Struslistener</listener-class>  </listener> -->  </web-app>

 

注意:運行測試,需要選中整個工程,run-onserver-選擇Tomcat7.0 server,即可

 

 點擊TestServlet

效果如下:

 

spring整合strus2的Hellowworld

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.