SpringMVC 基础配置

本文详细介绍了SpringMVC的工作原理及配置步骤,包括web.xml中DispatcherServlet的配置、spring-mvc.xml中的注解驱动配置、视图解析器的设置以及静态资源的处理等。

第一步: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";
}
}

【无人机】基于改进粒子群算法的无人机路径规划研究[和遗传算法、粒子群算法进行比较](Matlab代码实现)内容概要:本文围绕基于改进粒子群算法的无人机路径规划展开研究,重点探讨了在复杂环境中利用改进粒子群算法(PSO)实现无人机三维路径规划的方法,并将其与遗传算法(GA)、标准粒子群算法等传统优化算法进行对比分析。研究内容涵盖路径规划的多目标优化、避障策略、航路点约束以及算法收敛性和寻优能力的评估,所有实验均通过Matlab代码实现,提供了完整的仿真验证流程。文章还提到了多种智能优化算法在无人机路径规划中的应用比较,突出了改进PSO在收敛速度和全局寻优方面的优势。; 适合人群:具备一定Matlab编程基础和优化算法知识的研究生、科研人员及从事无人机路径规划、智能优化算法研究的相关技术人员。; 使用场景及目标:①用于无人机在复杂地形或动态环境下的三维路径规划仿真研究;②比较不同智能优化算法(如PSO、GA、蚁群算法、RRT等)在路径规划中的性能差异;③为多目标优化问题提供算法选型和改进思路。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注算法的参数设置、适应度函数设计及路径约束处理方式,同时可参考文中提到的多种算法对比思路,拓展到其他智能优化算法的研究与改进中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值