springmvc学习总结——4

现在我们来看一个通过注解实现的用户的例子:
web.xml不变:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <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>
    <!-- Servlet 前端控制器   springmvc核心控制器 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!-- 配置一个spring提供的针对post的中文乱码问题 -->
    <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>

spring.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: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/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
      ">
      <!-- 配置Action -->
      <context:component-scan base-package="com.my.springmvc.user.ann"></context:component-scan>
      <!-- 映射器 可以省略-->
      <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean>
</beans>

重点看我们的控制器:

@Controller
@RequestMapping(value = "/user")
public class UserAction {
    /*
     * 用户注册
     */
    @RequestMapping(value = "/register")
    public String register(Model model) throws Exception{
        model.addAttribute("message", "增加员工成功");
        return "/jsp/success.jsp";
    }
    /*
     * 用户登录
     */
    @RequestMapping(value = "/logon")
    public String logon(Model model) throws Exception{
        model.addAttribute("message", "员工登录成功");
        return "/jsp/success.jsp";
    }
}

我们在类上和方法上的都加了@RequestMapping注解,两者一拼接就是:
/user/register。
1.之前我们写的业务方法是void类型,没有返回值,这里我们写一个String类型的返回值的业务方法。返回值是需要返回的地址的真实名称,如果是逻辑名称,需要我们配置视图解析器的前缀和后缀。
2.之前的业务方法没有参数,现在我们给业务方法加一个参数。Model,而不是ModelAndView。通过这个Model我们可以addAttribute,设置的值可以在转发的页面中通过EL表达式获取获取。
接下来看一下jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    pageContext.setAttribute("basePath", basePath);
%>
<body >
    <div align="center">
        <h2>用户注册</h2>
        <form action="${basePath }user/register.action" method="post">
            姓名:<input type="text" name = "userName"><br>
            薪水:<input type="text" name = "salary"><br>
            <input type="submit" value="注册">
        </form>
    </div>
    <hr>
    <div align="center">
        <h2>用户登录</h2>
        <form action="${basePath }user/logon.action" method="post">
            姓名:<input type="text" name = "userName"><br>
            <input type="submit" value="登陆 ">
        </form>
    </div>
</body>
</html>

success.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    pageContext.setAttribute("basePath", basePath);
%>
<body>
    成功<br>
    ${requestScope.message}<br>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值