比较笨,看了三遍才能理解敲对并正确运行:

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;
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
*
*/
@WebListener
public 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的更多相关文章

  1. Spring整合strus2简单应用总结

    本身strus2没接触过,所以这块学的一知半解,正常不整合的还没学(接着学) step: 1.创建web工程 2.在/WEB-INF/lib引入jar包 asm-3.3.jarasm-commons- ...

  2. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

  3. 使用Spring整合Quartz轻松完成定时任务

    一.背景 上次我们介绍了如何使用Spring Task进行完成定时任务的编写,这次我们使用Spring整合Quartz的方式来再一次实现定时任务的开发,以下奉上开发步骤及注意事项等. 二.开发环境及必 ...

  4. spring整合hibernate的详细步骤

    Spring整合hibernate需要整合些什么? 由IOC容器来生成hibernate的sessionFactory. 让hibernate使用spring的声明式事务 整合步骤: 加入hibern ...

  5. Spring整合Ehcache管理缓存

    前言 Ehcache 是一个成熟的缓存框架,你可以直接使用它来管理你的缓存. Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现.它 ...

  6. spring整合hibernate

    spring整合hibernate包括三部分:hibernate的配置.hibernate核心对象交给spring管理.事务由AOP控制 好处: 由java代码进行配置,摆脱硬编码,连接数据库等信息更 ...

  7. MyBatis学习(四)MyBatis和Spring整合

    MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...

  8. Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来

    转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...

  9. Spring整合HBase

    Spring整合HBase Spring HBase SHDP § 系统环境 § 配置HBase运行环境 § 配置Hadoop § 配置HBase § 启动Hadoop和HBase § 创建Maven ...

随机推荐

  1. A1020. Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  2. ASP.NET MVC 网站优化之压缩技术

    压缩 html 可以去除代码中无用的空格等,这样可提高网站的加载速度并节省带宽. 实现 ActionFilter 来完成 html 的压缩 public class WhitespaceFilterA ...

  3. redux与react-redux

    Redux是一个数据状态管理插件,论是移动端还是 pc 端,当你使用 React 或者 vue 开发组件化的 SPA 程序时, 组件之间共享信息是一个非常大的问题.在react开发中,使用 React ...

  4. 蛋白质结构模型和功能预测:I-TASSER工具的使用

    I-TASSER是一款用于预测蛋白质结构和功能的工具,网站链接:https://zhanglab.ccmb.med.umich.edu/I-TASSER/ 具体描述如下: I-TASSER (Iter ...

  5. java基础基础总结----- 关键字、标识符、注释、常量和变量、运算符、语句、函数、数组(三)

    Java语言基础组成:关键字.标识符.注释.常量和变量.运算符.语句.函数.数组 一.标识符 标识符是在程序中自定义的一些名称,由大小写字母[a-zA-Z],数字[0-9],下划线[ _ ],特殊字符 ...

  6. 2018acm-icpc江苏邀请赛后记

    这场比赛可真难. 不得不说矿大的环境大大出乎了我的意料,无论是校园面积还是基础设施都很好,唯一美中不足的大概是吃了一顿晚饭每个菜都有辣,幸好饭票发的很多,一个人四张饭票,一张饭票可以换3根香蕉,于是我 ...

  7. Python基础【day02】:字符编码(一)

    本节内容 1.字符编码与转码 1.关于中文2.注释3.转码 2.表达式for 循环 3.数据类型之数字 1.数字2.布尔值3.字符串4.列表5.元祖6.字典 一.字符编码与转码 python解释器在加 ...

  8. Windows计划任务提示 0xE0434352 错误

    写了一个计划任务每周去跑一个程序,但是并没有跑,报错是 0xE0434352,应该是没有找到路径(计划任务这么菜的吗)... 解决办法:双击启动程序 写上你当前程序的起始路径 然后在运行一下,就成功了

  9. sudo 找不到命令 go

    错误描述 环境 CentOS7.1 x64 golang 1.9.3 golang目录:/home/moonlightwatch/go/ 环境变量配置: # /etc/profile export G ...

  10. CentOS 使用 Xfce 桌面并通过 xrdp 登录

    基础环境 CentOS 7.1 最小化安装 安装步骤 以下步骤,均通过ssh连接到主机进行操作. 安装桌面支持 首先安装桌面支持 yum groupinstall "Server with ...