天天看点

从零开始的spring mvc,spring mvc简单配置

 首先,先新建一个web项目,然后在lib文件夹下加入相应的spring jar 包,我所建的web项目结构如下

从零开始的spring mvc,spring mvc简单配置

 之后就配置web.xml文件信息

在配置web.xml 中前置控制器的<servlet-name>必须与<servlet-mapping>的<servlet-name>一致,而且<init-param>下的<param-name>必须为contextConfigLocation,这是spring 规定好的,否侧就会报找不到默认xml文件错误,是因为名字定义错误,导致要导进来的spring.xml文件加载不进,然后系统就会加载默认的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_2_5.xsd"
		id="WebApp_ID" version="2.5">
	 
	  <!-- 首先配置前置控制器  -->
	  <servlet>
	  	<servlet-name>springTest</servlet-name>
	  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  	<!-- 配置Spring mvc 下的配置文件的位置和名称  -->
	  	<init-param>
	  		<param-name>contextConfigLocation</param-name>
	  		<param-value>classpath:springmvc.xml</param-value>
	  	</init-param>
	  	<load-on-startup>1</load-on-startup>
	  </servlet>
	  <servlet-mapping>
	  	<servlet-name>springTest</servlet-name>
	  	<url-pattern>/</url-pattern>
	  </servlet-mapping>
</web-app>
           

之后再src目录下新建一个spring.xml文件,这是为了前面web.xml加载的xml所建的一个spring.xml文件名字要一致,不然会报找不到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:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
	  <!-- 配置自动扫描的包 -->
	  <context:component-scan base-package="com.jackie.springmvc"></context:component-scan>
	  
	  <!-- 配置视图解析器  如何把handler 方法返回值解析为实际的物理视图 -->
	  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	  	<property name="prefix" value="/WEB-INF/views/"></property>
	  	<property name="suffix" value=".jsp"></property>
	  </bean>
</beans>
           

配置好这两个xml文件之后就基本上完成一个简单的spring工程,下面是代码与页面的编写

这个是index.jsp页面body中添加的链接

<body>
    <a href="success/helloworld" target="_blank" rel="external nofollow" >hello world</a>
  </body>
           

这是点击链接跳转成功的jsp页面

<body>
    This is my success JSP page. <br>
  </body>
           

做完页面之后就是代码部分

@Controller
@RequestMapping("/success")
public class HelloWorld {
	
	/**
	  * 1. 使用RequestMapping注解来映射请求的URL
	  * 2. 返回值会通过视图解析器解析为实际的物理视图, 对于InternalResourceViewResolver视图解析器,会做如下解析
	  * 通过prefix+returnVal+suffix 这样的方式得到实际的物理视图,然后会转发操作
	  * "/WEB-INF/views/success.jsp"
	  * @return
	  */
	@RequestMapping(value="/helloworld",method=RequestMethod.GET)
	public String hello(){
		System.out.println("hello world");
		return "success";
	}
}
           

最后把项目部署到tomcat中,输入链接进行测试

继续阅读