java:Springmvc框架1(基本配置,注解配置,转换器引入)

1.springmvc01:(基本配置)

  java:Springmvc框架1(基本配置,注解配置,转换器引入)

  

  web.xml:

<?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>springmvc01</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> <!-- spring mvc的前端控制器,类似struts2的核心过滤器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过contextConfigLocation参数指定配置文件的位置,默认在WEB-INF/查找名称为 [servlet-name]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> <!-- servlet随web容器而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

  springmvc.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <!-- 处理器映射器 -->
<!-- 根据bean的name进行查找Handler处理器 将请求的url配置在bean的name中 -->
<!-- 这是一个默认的映射处理器,即使不配置,那么也是默认就是这个 -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> <!-- HandlerAdapter适配器 -->
<!-- 注意:这个适配器不是必须配置的,这是默认的、他在servlet容器已启动就被加载-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 配置自定义Handler控制器 -->
<!-- Handler后端控制器 -->
<!-- 配置controller ,使用beanName:name="/hello.action"进行进行请求映射匹配-->
<bean name="/hello.action" class="cn.zzsxt.controller.HelloController"></bean> <!-- 配置视图解析器 -->
<!-- InternalResourceViewResolver:支持JSP视图解析 -->
<!-- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包; -->
<!-- prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为: -->
<!-- 前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,-->
<!-- 则最终返回的jsp视图地址 "WEB-INF/view/hello.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

  HelloController.java:

package cn.zzsxt.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
/**
* 后端控制器,类似与struts2的业务逻辑action
* @author Think
*
*/
public class HelloController implements Controller {
/**
* 处理用户请求的方法,类似struts2中action的execute方法
*/
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
//处理用户请求
ModelAndView mv = new ModelAndView();
mv.addObject("hello", "hello,spring mvc !");//request.setAttribute("hello","hello,spring mvc!");
// mv.setViewName("/WEB-INF/jsp/hello.jsp");
mv.setViewName("hello");//视图的逻辑名称
return mv;
} }

  index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<a href="hello.action">hello springmvc!</a>
</body>
</html>

  

2.springmvc2:(注解配置)

  springmvc.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: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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解扫描!!!-->
<context:component-scan base-package="cn.zzsxt.controller"></context:component-scan> <!-- 注解映射器
说明 :对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配
ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method
-->
<!--
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-->
<!-- 注解适配器
说明:注解式处理器适配器,对标记@ResquestMapping的方法进行适配。
-->
<!--
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- springmvc使用<mvc:annotation-driven> -->
<!-- 自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter, -->
<!-- 可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 配置视图解析器 -->
<!-- InternalResourceViewResolver:支持JSP视图解析 -->
<!-- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包; -->
<!-- prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为: -->
<!-- 前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,-->
<!-- 则最终返回的jsp视图地址 "WEB-INF/view/hello.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>

  web.xml:

<?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>springmvc01</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> <!-- spring mvc的前端控制器,类似struts2的核心过滤器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过contextConfigLocation参数指定配置文件的位置,默认在WEB-INF/查找名称为 [servlet-name]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> <!-- servlet随web容器而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

  HelloController.java:

package cn.zzsxt.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
/**
* @Controller:控制器
* @RequestMapping:访问该处理器的映射路径。
* 如果在类的上边添加该注解,类似struts2的包的命名空间(窄化路径)
* 如果在业务方法的上方添加该注解,代表方法该方法对应的url
*
* /hello/aa.action--->aa()
* /hello/bb.action--->bb()
*
*
*/
@Controller
@RequestMapping("/hello")
public class HelloController {
//返回值为ModelAndView
@RequestMapping(value="/aa",method=RequestMethod.GET)
public ModelAndView aa(){
ModelAndView mv = new ModelAndView();
mv.addObject("hello","springmvc注解开发示例!");
mv.setViewName("hello");
return mv;
} //返回值为void
@RequestMapping("/bb")
public void bb(HttpServletRequest request,HttpServletResponse response){
request.setAttribute("hello", "返回值为void的controller方法!");
try {
request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} //返回值为String(逻辑视图名称),默认使用的请求转发
@RequestMapping("/cc")
public String cc(HttpServletRequest request){
request.setAttribute("hello", "返回值为String的controller方法!");
return "hello";// /WEB-INF/jsp/hello.jsp
} //重定向redirect:
@RequestMapping("/dd")
public String dd(HttpServletRequest request){
request.setAttribute("hello", "返回值为String的controller方法!");
// return "forward:hello";//如果请求转发到controller中的方法(类似与struts2的chain),需要在前面添加forward
return "redirect:/WEB-INF/jsp/hello.jsp";// /WEB-INF/jsp/hello.jsp }
}

3.springmvc03:(转换器引入,日期转换)

java:Springmvc框架1(基本配置,注解配置,转换器引入)

  

  web.xml:

<?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>springmvc01</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> <!-- spring mvc的前端控制器,类似struts2的核心过滤器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 通过contextConfigLocation参数指定配置文件的位置,默认在WEB-INF/查找名称为 [servlet-name]-servlet.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param> <!-- servlet随web容器而启动 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping> <!-- 解决post提交乱码问题 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

  springmvc.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: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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解扫描!!!-->
<context:component-scan base-package="cn.zzsxt.controller"></context:component-scan> <!-- 配置视图解析器 -->
<!-- InternalResourceViewResolver:支持JSP视图解析 -->
<!-- viewClass:JstlView表示JSP模板页面需要使用JSTL标签库,所以classpath中必须包含jstl的相关jar包; -->
<!-- prefix 和suffix:查找视图页面的前缀和后缀,最终视图的址为: -->
<!-- 前缀+逻辑视图名+后缀,逻辑视图名需要在controller中返回ModelAndView指定,比如逻辑视图名为hello,-->
<!-- 则最终返回的jsp视图地址 "WEB-INF/view/hello.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean> <!-- 注解映射器
说明 :对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配
ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method
-->
<!--
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
-->
<!-- 注解适配器
说明:注解式处理器适配器,对标记@ResquestMapping的方法进行适配。
-->
<!--
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- springmvc使用<mvc:annotation-driven> -->
<!-- 自动加载RequestMappingHandlerMapping和RequestMappingHandlerAdapter, -->
<!-- 可用在springmvc.xml配置文件中使用<mvc:annotation-driven>替代注解处理器和适配器的配置。 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven> <!-- 自定义类型转换器 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="cn.zzsxt.converter.CustomDateConverter"></bean>
</list>
</property>
</bean>
</beans>

  UserController.java:

package cn.zzsxt.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import cn.zzsxt.entity.Userinfo;
/**
* 约定大于配置
* @author Think
*
*/
@Controller
@RequestMapping("/user")
public class UserController { //根据用户请求访问/WEB-INF/jsp/register.jsp
@RequestMapping(value="/register",method=RequestMethod.GET)
public String register(){
return "register";
} @RequestMapping(value="/register2",method=RequestMethod.GET)
public String register2(){
return "register2";
}
//@RequestParam(name="uname"):参数绑定的注解,通过该注解可以解决参数名称与controller中形参名称不一致的问题。
// @RequestMapping(value="/register",method=RequestMethod.POST)
// public String register(@RequestParam(name="userName",required=true) String uname,@RequestParam(name="userPass") String upass,Model model){
// System.out.println("执行了用户注册,用户名:"+uname+",密码:"+upass);
// model.addAttribute("userName", uname);//request.setAttribute("userName",uname);
// return "success";
// }
@RequestMapping(value="/register",method=RequestMethod.POST)
public String register(String userName,String userPass,Model model){
System.out.println("执行了用户注册,用户名:"+userName+",密码:"+userPass);
model.addAttribute("userName", userName);//request.setAttribute("userName",userName);
return "success";
} @RequestMapping(value="/register2",method=RequestMethod.POST)
public String register2(Userinfo user,Model model){
System.out.println("执行了用户注册2,用户名:"+user.getUserName()+",密码:"+user.getUserPass()+",生日:"+user.getBirthDay());
model.addAttribute("user", user);//request.setAttribute("userName",userName);
return "success2";
}
}

  CustomDateConverter.java:

package cn.zzsxt.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.springframework.core.convert.converter.Converter;
/**
* 自定义类型转换器
* @author Think
*
*/
public class CustomDateConverter implements Converter<String, Date> { @Override
public Date convert(String source) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(source);
return date;
} catch (ParseException e) {
e.printStackTrace();
}
return null;
} }
上一篇:redis配置文件中常用配置详解


下一篇:mysql -- 远程访问mysql的解决方案