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

Spring Mvc的xml配置和测试

2017-10-25 19:04 225 查看
1.配置文件(web.mxl和***-servlet.xml,这里是springmvc-servlet.xml)

web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMvc1</display-name>

<servlet>
<servlet-name>springmvc</servlet-name>
<!-- servlet分发器 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 进入servletmvc框架的匹配规则 -->
<url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>


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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 启动spring包扫描 -->
<context:component-scan base-package="com.gbl.controller"/>

<!-- 处理静态资源 -->
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

<!-- 配置视图解析器开始 前面说的前缀和后缀-->
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 配置视图解析器结束 -->

<!-- 配置文件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

<!-- one of the properties available; the maximum file size in bytes -->
<property name="maxUploadSize" value="100000"/>
</bean>

<!-- 找到控制层的类 -->
<bean id="user" class="com.gbl.controller.UserController"></bean>

</beans>


2.控制层Controller

//通过注解找到这个类文件
@Reques
4000
tMapping(value = "/user")
public class UserController{
@RequestMapping(value = "/hello")
public String hello(){
return "hello"; //通过springmvc-servlet.xml配置视图解析器,给字符串
//加前缀和后缀,然后访问这个jsp文件
}

@RequestMapping(value="login")
//参数名必须与前台的name属性一样
//直接封装User对象
//向页面传值 用ModelMap或者用request、session
public String login(User user,ModelMap map){
System.out.println("进入login Controller");
//使用ModelMap传值,页面${uname},${password}获取
map.put("uname", user.getUname());
map.put("password", user.getPassword());
//redirect 重定向;forward 转向
return "redirect:loginSuccess.html";
}

@RequestMapping(value="loginSuccess.html")  //假装是html
public String loginSuccess(){
System.out.println("进入loginSucess.html Controller");
return "loginSuccess";
}

public String upload(@RequestParam("upload")MultipartFile mf,
HttpServletRuquest request){
//获取项目在tomcat里的全路径
String path = request,getServletContext().getRealPath("/fileUpload");
//request.getContextPath(); 从项目名开始的路径
//request.getServletContext().getResource("/"); //file:项目在tomcat里的全路径
//mf.getName(); 上传文件的前台name属性值
//mf.getOriginaFilename();  文件名(包括后缀)
mf.transferTo(new File(path+"/"+mf.getOriginalFilename()));
//request.getSession().setAttribute("upload",img.getOriginalFilename()); 把文件地址放进session里
return "页面";

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc xml