`#Springmvc中的部分配置
SpringMVC配置–在web.xml中搭建
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 1.注册springmvc核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 设置加载应用上下文路径,默认为WEB-INF/<servlet-name>-context.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 2.注册ContextLoaderListener -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:root.xml</param-value>
</context-param>
<!-- 1和2会各自加载一个Spring应用上下文,可以分别设置xml文件地址 -->
<!-- 注册spring核心编码过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
">
<import resource="xxxxx"/>
</beans>
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- Action,让springioc容器去扫描带@Controller的类 -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.app23"/>
</beans>
SpringMVC配置–设置web.xml基于java的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 指定使用java类进行配置 -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContex
</param-value>
</context-param>
<!-- 指定根配置类 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 设置根配置类的类路径 -->
<param-value>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.RootConfigClass
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 指定Springmvc核心控制器类 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定使用java类进行配置 -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.
AnnotationConfigWebApplicationContex
</param-value>
</init-param>
<!-- 指定java配置类的类路径 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.WebConfigClass
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
配置类具体参见P141
Springmvc中的映射器、适配器、视图解析器
-----------------------------全部通过xml文件进行配置---------------------------------
<!-- 控制器(程序员) -->
<bean id="HelloActionID" name="/hello.action" class="cn.itcast.javaee.springmvc.base.HelloAction"></bean>
<!-- 映射器(框架) -->
<!-- 1.BeanNameUrlHandler(默认)(一个路径对应一个Action) -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean>
<!-- 2.SimpleUrlHandler(多个路径对应同一个Action) -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/x1.action">HelloActionID</prop>
<prop key="/x2.action">HelloActionID</prop>
<prop key="/x3.action">HelloActionID</prop>
</props>
</property>
</bean>
<!-- 控制器与映射器搭配使用 -->
<!-- 控制器(框架) -->
<!-- ParameterizableViewController,如果请求是/hello.action的请求路径,则直接跳转到/jsp/success.jsp页面,不经过程序员定义的控制器Action -->
<bean name="/index.action" class="org.springframework.web.servlet.mvc.ParameterizableViewController">
<property name="viewName" value="/index.jsp"/>
</bean>
<!-- 适配器(框架),控制类需要实现Controller接口-->
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"></bean>
<!-- 视图解析器(框架) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 真实路径可以不配置 -->
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
通过注解配置控制器(推荐)
<!-- Action控制器,启用注解扫描(必须) -->
<context:component-scan base-package="cn.itcast.javaee.springmvc.helloannotation"/>
<!-- 基于注解的映射器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<!-- 基于注解的适配器(可选) -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<!-- 添加JSON转换功能 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<!-- 视图解析器(可选)(如果为逻辑地址则需要配置) -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"/>
@Controller
@RequestMapping(value="/user")
public class UserAction {
//以JSON格式返回对象
@RequestMapping(value="/add")
public @ResponseBody User add(User user) throws Exception{
System.out.println(user.getId()+":"+user.getName()+":"+user.getSal());
return user;
}
}
package cn.itcast.javaee.springmvc.helloannotation
@Controller //指明当前类为控制器类。自动创建对象并加入ioc容器
@RequestMapping(value="/user") //配置一级映射路径
public class UserAction {
/**
* 用户注册,只能接收POST请求
*/
@RequestMapping(method=RequestMethod.POST,value="/register")
public String registerMethod(Model model,String username,String salary) throws Exception{
System.out.println("用户注册-->" + username + ":" + salary);
model.addAttribute("message","员工注册成功");
return "/jsp/success.jsp"; //真实地址
/* 可以用redirect: 或者 forward: 标记,会自动解析进行重定向或者转发
例: return "forward:/user/add.action";
*/
}
/**
* 用户登录,即能接收POST请求,又能接收GET请求
*/
@RequestMapping(value="/login") //配置二级映射路径
public String loginMethod(Model model,String username) throws Exception{
System.out.println("用户登录-->" + username);
model.addAttribute("message","员工登录成功");
return "/jsp/success.jsp";
}
//将参数自动注入相应的对象
@RequestMapping(value="/addUser")
public void testBinderOuput(@ModelAttribute User user, HttpServletRequest request, HttpServletResponse response){
System.out.println(user);
}
}
public Class User{
private String name;
private int sex;
private String address;
private int id;
请求路径:
localhost/user/addUser 现在需要将参数自动传入user对象中去
如果后台表单中的name=”name” value=”test”
那么user.name=”test”;
如果后台表单中的name=”user.name” value=”test”
那么user.name=null;
默认情况下springMVC是不支持user.name这种传参方式的。
——————多个对象中有相同属性的情况下———————
如果有个对象manager中也有name这个相同的属性,那么可以采用user.name,manager.name来传输参数。
1.在controller中添加一个前缀绑定:
@InitBinder("manager")
public void initBinder1(WebDataBinder binder) {
binder.setFieldDefaultPrefix("manager.");
}
@InitBinder("user")
public void initBinder2(WebDataBinder binder) {
binder.setFieldDefaultPrefix("user.");
}
2.设计一个包装类Bean
Bean中有User和Manager两个成员变量
此时可以通过user.name manager.name直接向后台传递参数
@RequestMappting("/getBean")
public void testBinderOuput(@ModelAttribute Bean bean, HttpServletRequest request, HttpServletResponse response){
User user = bean.getUser();
Manager manager = bean.getManager();
}
Springmvc将提交给后台的参数自动注入到方法的参数中去,是通过方法参数的名字来识别的,但不是通过反射实现的。
通过获取该方法对应的Method对象的class对象(method.getClass()),然后将该class对象转为输入流读取到参数的具体名称后实现的.