1.开发环境
myeclipse 8.5 tomcat 5.5 jdk 1.6
2.又见hello world之j2se版。
http://kin111.blog.51cto.com/738881/171031/
通过xml对beans的描述,使用spring框架动态的反射出所需要的实体类。
首先使用myeclipse新建一个web工程,然后添加spring Capabilities。将自动生成的applicationContext.xml放在src目录中。新建两个类:HelloWorld和SpringClient.代码如下:
/** * * @author jefferyxu * */ public class HelloWorld { public void sayHello () { System.out.println("Hello World!"); } }
/** * * @author jefferyxu * */ import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringClient { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 这里需要和配置文件中的名字相对应 HelloWorld hello = (HelloWorld)applicationContext.getBean("sayHello"); hello.sayHello(); } }
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"> <!-- 这里添加自己的beans --> <bean id="sayHello" class="HelloWorld"></bean> </beans>
文件的目录结构如下:
直接运行SpringClient,可能产生如下的warnings:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). log4j:WARN Please initialize the log4j system properly.
解决的办法就是src目录中添加log4j.properties 文件。文件内容如下:
log4j.rootLogger=INFO, stdout, logfile log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.RollingFileAppender log4j.appender.logfile.File=${ideashopx.root}/WEB-INF/logs/ideashopx.log log4j.appender.logfile.MaxFileSize=1024KB # Keep three backup files. log4j.appender.logfile.MaxBackupIndex=3 # Pattern to output: date priority [category] - message log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
再次运行SpringClient程序,程序只是简单的输出hello world这个亲切的字符串。
3.hello world之j2ee web版。
http://maestric.com/doc/java/spring/hello_world
In 'WEB-INF/web.xml', we declare Spring DispatcherServlet and map '*.html' URLs to it:
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> jsp/index.jsp </welcome-file> </welcome-file-list> </web-app>
Let's now create the Spring configuration file 'WEB-INF/springmvc-servlet.xml' (name based on the servlet name above):
<?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-2.5.xsd"> <bean name="/hello_world.html" class="springmvc.web.HelloWorldController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
Map URL /hello_world.html to Controller HelloWorldController
Declare View Resolver: when view 'view_name' is called (from the Controller), the file '/jsp/view_name.jsp' will be used
在myeclipse中可以使用下面的方法生成简单的模板:
Let's create the Controller 'WEB-INF/src/springmvc/web/HelloWorldController.java':
package springmvc.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloWorldController implements Controller { public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String aMessage = "Hello World MVC!"; ModelAndView modelAndView = new ModelAndView("hello_world"); modelAndView.addObject("message", aMessage); return modelAndView; } }
Now the view: 'jsp/hello_world.jsp':
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <body> <p>This is my message: ${message}</p> </body> </html>
将整个的web工程deploy之后,打开:http://localhost:8180/springmvc/hello_world.html,如果一切正常的话,将显示“
This is my message: Hello World MVC!”字符串(由controller传递给页面)。
spring的hello world完毕,其中的主要意义是了解整个spring开发流程,忽略了其中的很多的细节的问题。