您的位置:首页 > 编程语言 > Java开发

Spring MVC环境搭建、一键式配置方法(注解)以及参数传递

2017-12-12 22:15 405 查看
一:web.xml配置Servlet

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring-mybatis</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!-- 配置springmvc核心控制器DispatcherServlet,springmvc的入口,springmvc本质就是servlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<!-- 通过设置contextConfigLoaction来指定springmvc配置文件的位置 -->
<param-name>contextConfigLoaction</param-name>
<param-value>classpath:springmvc-servlet.xml</param-value>
</init-param>
<!-- 自动启动加载此servlet -->
<load-on-startup>1</load-on-startup>
</servlet>

<!-- 截获并处理该项目所有URL请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

</web-app>

二:创建Spring MVC的配置文件springmvc-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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<!-- 扫描包,实现注解驱动Bean的定义,同时将Bean自动注入容器中使用,使标注了SpringMvc注解的Bean生效 -->
<context:component-scan base-package="cn.mySmbms.controller"/>
<!-- 配置<mvc:annotation-driven/>完成对@Controller和@RequestMapping等注解的支持 -->
<mvc:annotation-driven/>

<!-- 配置视图解析器,完成视图的对应 -->
<!-- 处理请求后需要渲染输出,这个任务由视图实现(此处为jsp),需要确定指定的请求需要使用哪个视图进行请求结果的渲染输出 -->
<!-- DispatcherServlet会查找到一个视图解析器,将控制器返回的逻辑视图名称转换成渲染结果的实际视图 -->
<!-- 如下面定义的视图解析器,通过配置prefix(前缀)和suffix(后缀)将视图逻辑名解析为/WEB-INF/jsp/<viewName>.jsp -->
<bean class
4000
="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

</beans>

三:创建Controller

package cn.mySmbms.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller//可处理HTTP请求的控制器
@RequestMapping("/indexController")//避免@RequestMapping冲突
public class IndexController {

//表示方法与那个请求URL来对应,此处的URL为/index,限定了index()将处理所有来自于/index”的请求(相对于web容器部署根目录)
//参数传递:required表示参数是否必须,默认true
@RequestMapping("/index")
public ModelAndView index(@RequestParam(value="username",required=false) String username) {
System.out.println("index()执行,username:"+username);
ModelAndView modelAndView=new ModelAndView();
modelAndView.addObject("username",username);//返回前端的模型数据,前端获取${username}
modelAndView.setViewName("index");//指定逻辑视图名
return modelAndView;
}
//Model对象入参(Map也行)
/*public String index(@RequestParam(value="username",required=false) String username,Model model) {
System.out.println("index()执行,username:"+username);
model.addAttribute("username",username);//前端获取${username}
model.addAttribute(username);//key为对象的类型,此处为String,前端获取${string},javaBean同理
return "index";//逻辑视图名
}*/

}


四:创建视图view

目录:/WEB-INF/jsp/index.jsp


五:访问测试

URL:http://localhost:6060/spring-mybatis/indexController/index?username=admin
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐