一、SpringMVC概述
1、是MVCII模式实现的框架技术之一
2、核心组件
(1)DispatcherServlet类[dɪˈspætʃə(r)]由Spring框架提供
(2)ModelAndView视图模型包装类,用于设置要请求的视图路径,一般在后台运行
(3)注解@RequestMapping(value="访问路径"),RequestMapping--请求映射
(4)@Contraller用于描述控制器组件。
(5)springmvc-servlet.xml文件,用于配置控制器扫描,将注解的控制器组件设置到Spring容器中
3、Springmvc的开发步骤
(1)在当前工程引入Spring组件包
(2)编写实体类、业务类和控制器类,并设置注解注入的方式
实体类Users.java
package com.bean;
import java.io.Serializable;
public class Users implements Serializable{
private String uname;
private String passwd;
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(String uname, String passwd) {
super();
this.uname = uname;
this.passwd = passwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
业务类IUsersBiz.java
package com.biz;
import com.bean.*;
public interface IUsersBiz {
public boolean check(Users us);
}
业务类UsersBiz.java
package com.biz;
import org.springframework.stereotype.Service;
import com.bean.Users;
@Service
/**
* @Service相当于
* <bean id="usersBiz" class="com.biz.UsersBiz"/>
* */
public class UsersBiz implements IUsersBiz {
public boolean check(Users us) {
if(us!=null){
if(us.getUname()!=null&&!us.getUname().trim().equals("")
&&us.getPasswd()!=null&&!us.getPasswd().equals("")){
return true;
}
}
return false;
}
}
控制器类UsersAction.java
package com.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.bean.*;
import com.biz.*;
@Controller
public class UsersAction {
@Autowired//依赖关联对象的注入
/**
* @Autowired替代
* <bean id="xxx" class="com.action.UsersAction">
* <property name="usersBiz" ref="usersBiz"/>
//ref的usersBiz是UsersBiz中@Service中usersBiz
* </bean>
* */
private IUsersBiz usersBiz;
public IUsersBiz getUsersBiz() {
return usersBiz;
}
public void setUsersBiz(IUsersBiz usersBiz) {
this.usersBiz = usersBiz;
}
@RequestMapping(value="check_Users.do")
/**
* @RequestMapping(value="check_Users.do")
* 相当于servlet配置中的url-patten路径映射
* <servlet-mapping>
* <servlet-name>xxx</servlet-name>
* <url-partten>/servlet/xxx</url-partten>
* <servlet-mapping>
* 请求路径的配置
* */
public String check(HttpServletResponse rep,HttpServletRequest request,Users us){
System.out.println("action已经动作!.......");
boolean flag=usersBiz.check(us);
if(flag){
request.setAttribute("us", us);
return "loginok.jsp";
}
return "fail.jsp";
}
}
(3)配置applicationContext.xml文件,加入注解扫描组件的上下文支持。并扫描注解的组件到Spring容器中
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
"
default-autowire="byName"
>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.biz"></context:component-scan>
<context:component-scan base-package="com.action"></context:component-scan>
</beans>
(4)在WEB-INF目录下编写springmvc-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
"
default-autowire="byName"
>
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.action"></context:component-scan>
</beans>
(5)在web.xml文件中加入spring的启动和中文转码的过滤器
<!-- spring启动配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置中文转码过滤器 -->
<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>
(6)在web.xml文件中配置springmvc的DispatcherServlet
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
(7)在WebRoot目录下编写jsp页面测试
index.jsp
<html>
<body>
用户登录 <br>
<form action="check_Users.do" method="post">
账号:<input type="text" name="uname"/><br>
密码:<input type="password" name="passwd"/><br>
<input type="submit" value="确定"/>
</form>
</body>
</html>
loginok.jsp
<html>
<body>
账号:${us.uname } <br>
密码:${us.passwd } <br>
</body>
</html>
fail.jsp
<html>
<body>
登录失败!<br>
</body>
</html>
二、SpringMvc的参数传递
1、在执行方法中使用参数对象进行传递
例如:
@RequestMapping(value="check_Users.do")
public String check(HttpServletResponse rep,HttpServletRequest request,Users us){
System.out.println("action已经动作!.......");
....
}
2、使用@RequestParam进行参数传递
例如:
@RequestMapping(value="findByName_Users.do")
public String findByName(@RequestParam(value="uname",required=false)){
....
}
路径:
http://.../.../findByName_Users.do?uname=xxx
或者
http://.../.../findByName_Users.do
3、在@RequestMapping中指定传递的参数
例如:
@RequestMapping(value="findByName_Users.do",params="uname")
public String findByName(HttpServletResponse rep,HttpServletRequest request){
....
}
注意 :uname参数值必须传递,否则出错