注意:基于注解式编程的Spring mvc 与普通的Spring mvc 最大的区别在于Spring框架的配置文件不同(首先头文件就不同)
前台页面只是一级请求(列子:login.do)
web.xml 配置:
<!-- 注册请求分发器 -->
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- dispatcherServlet 会默认寻找web-inf 路径下的myServlet-servlet.xml!我们将此路径重定向到spring的配置文件!-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</init-param>
</servlet>
<!-- 设置过滤请求 -->
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 设置编码,防止后台拿前台传的中文参数出现乱码 -->
<filter>
<filter-name>springUtf8Encoding</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>springUtf8Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
前台index页面:
<body>
<form method="post" action="login.do">
<p>
用户名:
<input type="text" name="username">
</p>
<p>
密 码:
<input type="password" name="password">
</p>
<p>
<input type="submit" value="提交" name="button1">
</p>
</form>
</body>
注解式Spring mvc Spring配置文件applicationContext.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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<!-- 自动加载驱动 ,通过主键把相应的url映射到controller上-->
<mvc:annotation-driven/>
<!-- 设置哪些包需要使用注解-->
<context:component-scan base-package="com.spring.mvc"></context:component-scan>
</beans>
<!--这样配置,只要是com.spring.mvc包下面的所有action类都可以使用注解标签-->
后台UserAction类代码:
package com.spring.mvc;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;
import org.springframework.web.servlet.DispatcherServlet;
//声明该类为controller层
@Controller
<pre name="code" class="java" style="font-size:14px;">public class UserAction {
/**
* 处理前台登录请求
* @return
*/
@RequestMapping("/login.do")
public String login(String username,String password,Model model){
/*只能通过方法里面传和前台同名的参数来拿前台传递的值,不能像Struts框架那样通过定义类的属性来拿前台参数*/
System.out.println("登录成功");
System.out.println(username + " " + password);
model.addAttribute("msg", "登录成功");//带提示信息去前台
return "a.jsp";
}
}
注意事项:上面方法是前台页面只是一级请求
(一些大公司登录请求是分部门的)
下面来说说如果是url是多级请求怎么办(web.xml配置跟上面相同)
列如:(money/login.do)
前台index页面:
<form method="post" action="money/login.do?id=2">
<p>
用户名:
<input type="text" name="username">
</p>
<p>
密 码:
<input type="password" name="password">
</p>
<p>
<input type="submit" value="提交" name="button1">
</p>
</form>
</body></span>
注解式Spring mvc Spring配置文件applicationContext.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-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
>
<!-- 自动加载驱动 ,通过主键把相应的url映射到controller上-->
<mvc:annotation-driven/>
<!-- 设置哪些包需要使用注解 -->
<context:component-scan base-package="com.money.web"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
<property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"></property>
</bean>
</beans>
后台UserAction类代码:
package com.money.web;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import com.money.dao.Emp;
@Controller
@RequestMapping("/money")
//多级请求进方法时需要先将二级目录与真正的.do/.action请求分离,这里先进money目录
public class UserAction {
@Autowired //这个注解是为了获取request作用域(为了得到session作用域可以存登录信息)
public HttpServletRequest request;
@RequestMapping(value="/login.do",params="id=2")
/*这里的id=2只有与前台跳转是打问号带的参数id保持一致时才能进方法(也可以不使用)*/
public String login(Emp emp){
/*这里跟struts不同的地方是:我们后台用对象来拿前台传递的值时,不需要前台以emp.username这样的方式传递值
,应为只要前台name的值和emp对象属性的值保持相同时系统会自动识别*/
System.out.println("登录chengn");
request.getSession().setAttribute("msg", "admin");
System.out.println(emp.getUsername() + " " + emp.getPassword());
return "a";
/*应为是多级请求,所以不能再用一级请求a.jsp那样往前台跳了,应为这里如果写a.jsp真正跳转的是跳转到money/a.jsp这个页面,
所以我们通过对Spring配置文件applicationContext.xml 配置一个配置视图解析器 然后我们这里跳转只要只写a就行了,不用加.jsp后缀,
试图解析器会自动帮我们加,还会去掉之前的多级层 */
}
}
(完)
两个可以了解的事项:
A、在requestMapping中可以指定一个属性method,主要用来限定访问请求的类型为post还是get
@RequestMapping(value="/login.do",method=RequestMethod.POST)
public String login(){
System.out.println("登录成功");
return null;
}
上述表述该方法只能处理Post类型的login.do请求
B、
@RequestMapping(value="/login.do",params="id=1")
public String login(){
System.out.println("登录成功");
return null;
}
上述表述如果login.do请求中有id=1这么一个键值对,则可以访问下面的login方法!