您的位置:首页 > 编程语言 > Java开发

简单的Spring入门例子

2007-04-26 10:51 453 查看
应一位朋友的要求,写一个最简单的spring示例,使用spring的MVC,并应用了spring的依赖注入,实现简单应用,索性放在这里供还没入门的spring爱好者参考,初步感受一下spring应用(spring高手就不必看了,这里并没有涉及高级特性,比如与ORM框架的整合,事务管理,远程调用,代理等这些功能)

  spring至关重要的一环就是装配,即配置文件的编写,接下来我按刚才实际过程中一步步简单讲解。

  首先,要在web.xml中配置DispatcherServlet,它是作为Spring MVC的前端控制器.必须在web.xml中配置好,如下

<servlet>
<servlet-name>ntx</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
  实际上,spring的配置文件可以分切到多个xml文件,我们这个简单的示例就把它配置到ntx.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">

<beans
default-autowire="no"
default-lazy-init="false"
default-dependency-check="none"


<bean id="loginService" class="ntx.service.serviceimpl.LoginServiceImpl"/>

<bean id="loginController" class="ntx.controller.LoginController">
<property name="loginService">
<ref bean="loginService"/>
</property>
<property name="gotoUrl">
<value>/showResult.jsp</value>
</property>
</bean>

<bean id="SimpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/userLogin.do">loginController</prop>
</props>
</property>
</bean>
</beans>
  配置好上面的这些后,要在WEB-INF下要建立ntx-servlet.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
 <property name="prefix"><value></value></property>
 <property name="suffix"><value></value></property>
</bean>
</beans>
  接下来,要指明哪些请求将交给spring的DispatcherServlet来处理,所以在web.xml中添加<servlet-mapping>

<servlet-mapping>
 <servlet-name>ntx</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>
  为了能正确载入DispatcherServlet等配置文件,我们要在web.xml中配置一个上下文载入器ContextLoaderListener或者ContextLoaderServlet,我们这里为了兼容版本较低的Serlvet容器(实际上我采用的2.4),采用第二种:

<servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
 <load-on-startup>100</load-on-startup>
</servlet>
  这样就全部配置完毕了,当然,上面的ntx.xml是我在项目完成以后才配置完成的,这里不再多讲,有bean元素的配置大家可以参考有关资料理解,很容易理解的,下面再给出完整的web.xml配置以及java<?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">
<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/ntx.xml</param-value>
</context-param>
<servlet>
 <servlet-name>ntx</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 <load-on-startup>1</load-on-startup>
</servlet>
<servlet>
 <servlet-name>context</servlet-name>
 <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
 <load-on-startup>100</load-on-startup>
</servlet>
<servlet-mapping>
 <servlet-name>ntx</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
  根据ntx.xml知道,总共有三个java文件,LoginController.java是控制器,继承了最简单的Controller(实际上spring有很多控制器供我们选择),接下来是一个简单控制器的源码:

/** *//**
* program NtxSpring
* date 2006-9-27
* @author 张逸轩
*/
package ntx.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import ntx.service.LoginService;

/** *//**@spring.bean id="loginController"
* @spring.property name="gotoUrl" value="/showResult.jsp"
* @spring.property name="loginService" ref="loginService"
*/

/** *//**
* 作用描述:spring示例
*
* 说明:spring示例,以上的spring标签方便使用xdoclet生成spring配置文件
*
* @author 张逸轩
* Copyright(c)2006 cleverfox
*/
public class LoginController implements Controller...{

private LoginService loginService ;
private String gotoUrl;
public ModelAndView handleRequest(HttpServletRequest request,HttpServletResponse response) throws Exception ...{
 String userName = request.getParameter("userName");
 this.getUserInfo(request, userName);
 return new ModelAndView(this.getGotoUrl());
}

private void getUserInfo(HttpServletRequest request,String userName)...{
 String userInfo = loginService.getUserInfo(userName);
 request.setAttribute("userInfo", userInfo);
}

public String getGotoUrl() ...{
 return gotoUrl;
}
public void setGotoUrl(String gotoUrl) ...{
 this.gotoUrl = gotoUrl;
}
public LoginService getLoginService() ...{
 return loginService;
}
public void setLoginService(LoginService loginService) ...{
 this.loginService = loginService;
}

}
  还有service层的接口以及实现,较简单。

package ntx.service;
public interface LoginService ...{
 public String getUserInfo(String userName);
}package ntx.service.serviceimpl;

import ntx.service.LoginService;

public class LoginServiceImpl implements LoginService ...{
 public String getUserInfo(String userName)...{
  return "你的名字是:" + userName;
 }
}
  好了,最后是两个jsp文件,一个index.jsp用来显示一个表单,输入名字,一个showResult.jsp用来显示结果,只贴出相关的代码:

<body>
This is my Test Spring page. <br>
<div>
<form method="post" action="/userLogin.do">
<input type="text" name="userName" size="30"/><br/>
<input type="submit" value="提交"/>
</form>
</div>
</body>

<body>
This is the Result: <br>
<c:out value="${userInfo}" default="没有结果"/>
</body>
  发布到tomcat或者其它Servlet容器可以正常使用,提交以后将显示:

  This is the Result:

  你的名字是:gavin



  这是一个简单的入门示例,希望有助于刚接触spring的人加深对spring的理解。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息