SpringMVC 基础配置

第一步:web.xmL 中配置转发器

<!-- 配置转发器(注: servlet是处理请求和响 应的,其包位于tomcat下,其中包含httpServletRequest和HttpServletResponse) -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--该初始化的参数含义为,把SpringMVC的sevlet定义到 spring-mvc.xml中,若没有该参数,则系统默认会在WEB-INF下寻找SpringMVC-servlet.xml(其中SpringMVC对应的是servlet-name) -->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 使用一般的配置文件:启动时先运行spring-mvc.xml -->
<!-- <param-value>classpath:spring-mvc.xml</param-value> -->
<!-- 使用注解来配置 启动时先运行springAnnotation-mvc.xml -->
<param-value>classpath:springAnnotation-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!--配置是指拦截以“/”结转尾的所有请求-->
<url-pattern>/</url-pattern>
</servlet-mapping>

第二步:配置xml文件:根据web.xml 中配置的“<param-value>classpath:springAnnotation-mvc.xml</param-value>”找到相应的xml文件并进行加载。

其中只有配置了xsi:schema Location 和xmlns,在xml中才能使用类似<mvc:annotation-driven/>中的mvc标签。

<?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:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd 
http://www.springframework.org/schema/jee 
http://www.springframework.org/schema/jee/spring-jee-4.1.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.1.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc  
      http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    <!-- 注解扫描包 ,若不配置,则请求是会找不到对应包下面的action-->
<context:component-scan base-package="com.cn.nipanlong.test.Controller.Annotation"></context:component-scan>

<!-- 开启注解方法一自动调用-相当于下面注释掉的两个方法开启注解自动调用1和2共同作用-->
<mvc:annotation-driven/>
<!-- 开启注解方法二自动调用1 -根据URL找方法-->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean> -->
<!-- 开启注解方法二自动调用2 -根据URL找类-->
<!--<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>-->

<!-- 定义跳转的文件的前后缀 ,视图模式配置,作用是将ModelAndView mv=new ModelAndView();mv.setViewName("helloworldController");
中的helloworldController 变为/WEB-INF/jsp/helloworldController.jsp -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- <property name="suffix" value=".jsp" /> -->
</bean>

<!-- 定义MultiActionController 解析器 -->
<!-- property中的action含义是:http://localhost:8080/SpringMVC/myMultiActionController?action=add 中的参数action来调用add方法 -->
<bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<property name="paramName" value="action"></property>
</bean>

<!-- 静态资源访问 :含义为把有/img(根目录下的img文件中的内容如:http://localhost:8080/SpringMVC/img/staticFile.png)下的请求不会被拦截,即不经过servlet发送请求-->
<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
</beans>

第三步:使用标签来对应前台请求的action

@Controller
public class UserController {
//两个参数,value 指的是action,method指请求方式,
//@RequestMapping(value="/user/addUser")无method参数,@RequestMapping(value="/user/addUser")即则不管前台发送什么请求(get/post/其它),都能够获得到
//@RequestMapping("/user/addUser") 或无(value= )效果是一样的
@RequestMapping(value="/user/addUser",method=RequestMethod.GET)
public ModelAndView addUser() {
String result="method addUser";
return new ModelAndView("/annotation.jsp","result",result);
}
//两个参数,value 指的是action,method指请求方式
@RequestMapping(value="/user/delUser",method=RequestMethod.GET)
public ModelAndView delUser() {
String result="method delUser";
return new ModelAndView("/annotation.jsp","result",result);
}
//其它写法也行
@RequestMapping(value="/user/otherUser",method=RequestMethod.GET)
public String delUser(HttpServletRequest request) {
String result="method delUser-其它写法";
request.setAttribute("result", result);
return "/annotation.jsp";
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值