SpringMVC-HelloWorld (XML)

Spring2.5.6开启了Spring的注解时代,简化了的xml配置,提高了开发效率;但是,对于Spring的初学者,xml配置更容易理解的Spring的Ioc特性,aop特性;本文使用Maven构建工具,构建了基于XML配置的SpringMVC示例;

使用的技术

  1. Spring 3.0.5.RELEASE
  2. Maven 3
  3. JDK 1.7
  4. eclipse Kepler

Maven的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gogo</groupId>
<artifactId>springmvc-xml</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring>3.0.5.RELEASE</spring>
</properties> <dependencies>
<!-- springmvc配置 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring}</version>
</dependency> <!-- spring-jdbc 配置-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring}</version>
</dependency> <!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
</project>

Servlet的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>springmvc-xml</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> <!-- Servlet配置 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SpringMVC-servlet.xml</param-value>
<!-- 默认是/WEB-INF/[servlet名字]-servlet.xml -->
</init-param>
<load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping> <!-- 编码处理过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

SpringMVC-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- Scan the JavaConfig -->
<!-- <context:component-scan base-package="com.cjt.springjdbc" /> -->
<!-- controller配置 -->
<bean name="/welcome.htm" class="com.cjt.springmvc_xml.controller.HelloController">
<property name="messageService">
<ref bean="messageService"></ref>
</property>
</bean> <!-- service配置 -->
<bean name="messageService" class="com.cjt.springmvc_xml.service.MessageService">
<!-- <property name="message" ref="message" /> -->
</bean> <!-- <bean name="message" class="com.cjt.springmvc_xml.model.Message">
<property name="content" value="你好,世界!" />
</bean> --> <!-- 视图解析配置 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
<!-- <property name="order" value="0" /> -->
</bean> <!-- 数据库配置 -->
<!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <context:property-placeholder location="classpath:jdbc.properties" /> --> </beans>

Controller层

package com.cjt.springmvc_xml.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; import com.cjt.springmvc_xml.service.MessageService; public class HelloController extends AbstractController{ private MessageService messageService; public MessageService getMessageService() {
return messageService;
} public void setMessageService(MessageService messageService) {
this.messageService = messageService;
} @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
// TODO Auto-generated method stub
ModelAndView model = new ModelAndView("HelloPage");
model.addObject("msg", messageService.getMessage().getContent());
return model;
} }

Service层

package com.cjt.springmvc_xml.service;

import com.cjt.springmvc_xml.model.Message;

public class MessageService {
/* private Message message; public Message getMessage() {
return message;
} public void setMessage(Message message) {
this.message = message;
} */ public Message getMessage() {
Message message = new Message();
message.setContent("你好,世界!");
return message;
} }

Mode层

package com.cjt.springmvc_xml.model;

public class Message {
private String content; public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} }
上一篇:HTML5入门4---HTML5 与 HTML4 同一网页的不同写法


下一篇:Java 解析XML的几种方法