java Sring-MVC简单配置和使用

来源:互联网 发布:鼠标知乎 编辑:程序博客网 时间:2024/05/22 00:00

1、所需要的依赖包

commons-logging-1.1.1.jar

spring-asm-3.1.1.RELEASE.jar
spring-beans-3.1.1.RELEASE.jar
spring-context-3.1.1.RELEASE.jar
spring-core-3.1.1.RELEASE.jar
spring-expression-3.1.1.RELEASE.jar
spring-web-3.2.8.RELEASE.jar

spring-webmvc-3.2.8.RELEASE.jar


2、WEB-INF/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_2_5.xsd"
version="2.5">


<servlet>
<servlet-name>spring-mvc</servlet-name>

<!-- springMVC的请求是由DispatcherServlet容器(请求前置控制器)管理分发到指定Controller处理

DispatcherServlet对象由spring-webmvc-3.2.8.RELEASE.jar

-->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>
<param-name>contextConfigLocation</param-name>

<!--默认/WEB-INFspring-mvc-servlet.xml-->
<param-value>resouce/spring-mvc-servlet.xml</param-value>
</init-param>


</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>

<!--前置控制器过虑请求,接受*.do后缀的请求-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

3、resouce/spring-mvc-servlet.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util" 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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd     
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<context:component-scan base-package="com.ldc" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

4、/linux/src/com/ldc/Controller/TestController.java控制器

<!--Controller注解,当开启了注解自动扫瞄,些注解下的类会自动注册为bean-->

@Controller
public class TestController {
@RequestMapping("test/hello.do")//请求映射:用户请求如:127.0.0.1/xxx/test/hello.do、如自动映射到/WEB-INF/jsp/Index.jsp(index是下面方法返回的值,WEB-INF/jsp/和.jsp是mvc配置文件所配置,与下方法的返回值拼接成一个映射路径,index.jsp显图)
public String test(){
return "index";
}
}

1 0
原创粉丝点击