springmvc框架的配置方法及详细说明
我们程序员都需要掌握各种框架结构,随着我们互联网的不断进步,很多功能和产品都在不断的发生变化,这个时候我们就必须要灵活的使用这些框架结构,下面是爱站技术频道小编介绍的springmvc框架的配置方法及详细说明,希望能帮到您。
一、Spring MVC环境搭建:(Spring 2.5.6 + Hibernate 3.2.0)
1. jar包引入
Spring 2.5.6:spring.jar、spring-webmvc.jar、commons-logging.jar、cglib-nodep-2.1_3.jar
Hibernate 3.6.8:hibernate3.jar、hibernate-jpa-2.0-api-1.0.1.Final.jar、antlr-2.7.6.jar、commons-collections-3.1、dom4j-1.6.1.jar、javassist-3.12.0.GA.jar、jta-1.1.jar、slf4j-api-1.6.1.jar、slf4j-nop-1.6.4.jar、相应数据库的驱动jar包
2. web.xml配置(部分)
spring org.springframework.web.servlet.DispatcherServlet 1 spring *.do org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:config/applicationContext.xml
3. spring-servlet.xml配置
spring-servlet这个名字是因为上面web.xml中
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
4. applicationContext.xml配置
二、详解
Spring MVC与Struts从原理上很相似(都是基于MVC架构),都有一个控制页面请求的Servlet,处理完后跳转页面。看如下代码(注解):
package controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import entity.User;
@Controller //类似Struts的Action
public class TestController {
@RequestMapping("test/login.do") // 请求url地址映射,类似Struts的action-mapping
public String testLogin(@RequestParam(value="username")String username, String password, HttpServletRequest request) {
// @RequestParam是指请求url地址映射中必须含有的参数(除非属性required=false)
// @RequestParam可简写为:@RequestParam("username")
if (!"admin".equals(username) || !"admin".equals(password)) {
return "loginError"; // 跳转页面路径(默认为转发),该路径不需要包含spring-servlet配置文件中配置的前缀和后缀
}
return "loginSuccess";
}
@RequestMapping("/test/login2.do")
public ModelAndView testLogin2(String username, String password, int age){
// request和response不必非要出现在方法中,如果用不上的话可以去掉
// 参数的名称是与页面控件的name相匹配,参数类型会自动被转换
if (!"admin".equals(username) || !"admin".equals(password) || age
以上4个方法示例,是一个Controller里含有不同的请求url,也可以采用一个url访问,通过url参数来区分访问不同的方法,代码如下:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test2/login.do") // 指定唯一一个*.do请求关联到该Controller
public class TestController2 {
@RequestMapping
public String testLogin(String username, String password, int age) {
// 如果不加任何参数,则在请求/test2/login.do时,便默认执行该方法
if (!"admin".equals(username) || !"admin".equals(password) || age
其实RequestMapping在Class上,可看做是父Request请求url,而RequestMapping在方法上的可看做是子Request请求url,父子请求url最终会拼起来与页面请求url进行匹配,因此RequestMapping也可以这么写:
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test3/*") // 父request请求url
public class TestController3 {
@RequestMapping("login.do") // 子request请求url,拼接后等价于/test3/login.do
public String testLogin(String username, String password, int age) {
if (!"admin".equals(username) || !"admin".equals(password) || age
大家掌握了springmvc框架的配置方法及详细说明,应该就能拥有很好的基础知识了,我们在熟练掌握后,便能运用自如这个技术,同时也希望大家能一如既往的支持js.aizhan.com。
