Springmvc部分
DsipatcherServlet部分
1.概述:Spring mvc 是基于Model2(就是jsp+javabean和jsp+servlet+javabean两种模式,前者是纯JSP,后者是简单的MVC, 是SUN公司对于MVC模式所先后推出的二种规范,第一种是JSP MODEL1,第二种则是JSP MODEL2. MODEL1在一定程序上实现了MVC,即JSP是控制层与表示层合二为一了,BEAN则为模式层.这样的效果显然不如将控制层单独划分出来的好.而MODEL2则是将控制层(SERVLET)单独的划分出来了,专门负责业务流程的控制,接受页面的请求,创建所需的JAVABEAN实例,并将处理后的数据再返回给JSP,这样的效果显然要好的多.)的技术的框架,利用处理器分离模型、视图和控制层,达到开发项目的低耦合目的,提高系统的灵活性、可复用性和可维护性。
2.首先来进行配置DsipatcherServlet的配置(WEB-INF/web.xml)
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--映射的配置,此处配置了spring的DispatcherServlet名字,这样他会默认加载名为/WEB-INF/spring-servlet.xml的配置文件-->
<servlet>
<servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!--將所有以*.htm結尾的请求交给 org.springframework.web.servlet.DispatcherServlet -->
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<!—其实这个不用配置,单位了服务器不报错这里还是配置了。因为没有applicationContext.xml文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-servlet.xml
</param-value>
</context-param>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
3.再来配置/WEB-INF/spring-servlet.xml文件
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<!—这些配置的意思是当controller返回视图是,会将返回的视图实体和/WEB-INF/example/和下所有以.jsp为结尾的文件名或是文件路径/文件名比对,匹配到就返回到对应的jsp视图页面进行渲染-->
<beanid="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="prefix">
<value>/WEB-INF/example/</value>
</property>
<propertyname="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
3.请求到对应的方法配置
(1)、基于配置文件的配置/WEB-INF/spring-servlet.xml
在/WEB-INF/spring-servlet.xml文件中加如下内容
<!-- 意思是将以/result.htm为结尾的请求交给 springapp.web.HelloController进行处理-->
<beanname="/result.htm" class="springapp.web.HelloController"/>
(2)、基于注解
HelloController类如下
package springapp.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import example.User;
@Controller//@Controller用来声明一个controller
publicclass HelloController {
@RequestMapping("/result")//@RequestMapping用来指定接受的请求
protected String login(HttpServletRequest request,
User user/*user是一個pojo类*/)throws Exception {
return"result";//newModelAndView("result");
/*直接在result.jsp用${user.name}*/
}
/**
* 使用@RequestParam注解来配置方法参数
* @param name
* @param password
* @param age
* @param date
* @return
* @throws Exception
@RequestMapping("/result")
//required=false是为了当前台name为空时不会抛异常
protected String login(@RequestParam(value="name",required=false)String name,
@RequestParam() String password,@RequestParam()intage,@RequestParam() String date) throws Exception {
System.out.println(password+age+password+date);
return "result";
}
*/
/**
* 使用@CookieValue绑定CookieValue,获取CookieValue
* @param sessionId
* @param age
* @return
* @throws Exception
@RequestMapping("/result")
protected Stringlogin(@CookieValue(value="sessionId",required=false)StringsessionId,@RequestParam()int age) throws Exception {
return "result";
}
*/
/**
* 其他
* @param session
* @return
* @throws Exception
@RequestMapping("/result")
protected ModelAndView login(HttpSession session) throwsException {
String message = session.getId() ;
return newModelAndView("result","message",message);
}
*/
}
待续….