springmvc helloworld

1.配置DispatcherServlet

在web.xml文件中,添加:

   <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>/</url-pattern>
   </servlet-mapping>

上述配置中,默认会在WEB-INF/目录下,查找springmvc-servlet.xml的配置文件,模式为:(servletname)-servlet.xml.找到该配置文件后,以此来加载应用程序上下文。

如果想要修改默认的配置文件,则可通过下面的方法来指定配置文件路径:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>

<listener>
   <listener-class>
      org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>

 

2.配置springmvc-servlet.xml

<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"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:component-scan base-package="com.xiaofan.hello.activiti" />

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/pages/" />
      <property name="suffix" value=".jsp" />
   </bean>

</beans>
 

3.定义控制器

@Controller
@RequestMapping("/hello")
public class HelloController{
 
   @RequestMapping(method = RequestMethod.GET)
   public String printHello(ModelMap model) {
      model.addAttribute("message", "Hello Spring MVC Framework!");
      return "hello";
   }

}

4.创建jsp视图

<html>
   <head>
   <title>Hello Spring MVC</title>
   </head>
   <body>
   <h2>${message}</h2>
   </body>
</html>

5.运行web测试

习惯使用jetty进行web测试,因为是maven项目,所以对pom.xml进行以下步骤的修改:

1.添加servlet依赖:

<!-- servlet依赖 -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>servlet-api</artifactId>
	<version>2.5</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>javax.servlet.jsp</groupId>
	<artifactId>jsp-api</artifactId>
	<version>2.1</version>
	<scope>provided</scope>
</dependency>
<dependency>
	<groupId>javax.servlet.jsp.jstl</groupId>
	<artifactId>jstl-api</artifactId>
	<version>1.2</version>
</dependency>

2.添加jetty插件:

<plugin>
	<groupId>org.mortbay.jetty</groupId>
	<artifactId>jetty-maven-plugin</artifactId>
	<version>8.1.14.v20131031</version>
	<configuration>
		<jvmArgs>-Dfile.encoding=UTF-8</jvmArgs>
		<scanIntervalSeconds>0</scanIntervalSeconds>
		<stopKey>foo</stopKey>
		<stopPort>9999</stopPort>
		<webApp>
			<contextPath>/activiti-hello</contextPath>
		</webApp>
		<connectors>
			<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
				<port>9090</port>
				<maxIdleTime>60000</maxIdleTime>
			</connector>
		</connectors>
	</configuration>
</plugin>

经过上述步骤,可以通过mvn jetty:run来运行测试了,在浏览器中输入http://127.0.0.1:9090/activiti-hello/hello,可以看到以下页面:

image

至此,springmvc基本框架的helloworld实例已经完成

总结

总结一下,开发springmvc的简单应用包括以下步骤:

  1. 在web.xml中配置DispatcherServlet,用它请求规划好的那类请求(如 *.do , *.action , / );
  2. 配置servletname-servlet.xml文件,以配置springmvc上下文环境;
  3. 编写Controller;
  4. 编写jsp页面;
  5. 运行jetty测试;
posted @ 2016-10-29 16:34  寻梦丄天涯  阅读(237)  评论(0编辑  收藏  举报