千锋逆战1903班Days17上课代码以及笔记

在千锋“逆战”学习第17天
今天学习了springmvc注解和非注解实现的两种方式
继续加油,奥利给!
课程作业笔记

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
	<!--解决乱码-->
	<filter>
		<filter-name>characterEncoding</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>characterEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<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:springmvc.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

springmvc.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.xsd
       http://www.springframework.org/schema/mvc
       https://www.springframework.org/schema/mvc/spring-mvc.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解-->
    <mvc:annotation-driven/>
    <!--配置缺省的servlet处理器,静态资源可以直接被访问-->
    <mvc:default-servlet-handler />
    <!--不使用注解-->
    <!--<bean name="/show" class="controller.ShowControllerNoAnnotation"></bean>-->
    <!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>-->
    <!--使用注解-->
    <!--启用注解扫描-->
    <context:component-scan base-package="controller"></context:component-scan>

</beans>

这里需要注意这两个注解

<mvc:annotation-driven />
<mvc:default-servlet-handler />

如果单独的写<mvc:default-servlet-handler />运行时会报404错误,具体原因请点击这个连接这个连接

非注解访问控制器

package controller;

import bean.CheckUser;
import bean.User;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import utils.ParameterCheck;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author xw
 * @Title:
 * @Package
 * @Description:
 * @date 2020/3/3 000319:04
 */
public class ShowControllerNoAnnotation implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String a = request.getParameter("age");
        int age = a.trim()==""?0:Integer.parseInt(request.getParameter("age"));
        String hobby = request.getParameter("hobby");
        //校验值是否符合条件
        ParameterCheck parameterCheck = new ParameterCheck();
        CheckUser checkUser = new CheckUser(name, password, age, hobby);
        List<String> errors = parameterCheck.check(checkUser);
        //创建视图
        ModelAndView view = new ModelAndView();
        if (errors.size()!=0){
            //参数不合法
            view.setViewName("index.jsp");
            view.getModel().put("errors",errors);
            return view;
        }
        //验证成功后跳转
        User user = new User(1, name, password, age, hobby);
        view.setViewName("WEB-INF/view/show.jsp");
        view.getModel().put("user",user);
        return view;
    }
}

需要实现Controller接口以及在配置文件中声明该bean

注解方式访问控制器

package controller;

import bean.CheckUser;
import bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import utils.ParameterCheck;

import java.util.List;

/**
 * @author xw
 * @Title:
 * @Package
 * @Description:
 * @date 2020/3/3 000319:46
 */
@Controller
@RequestMapping("/showController2")
public class ShowControllerHasAnnotation {

    //当浏览器访问该方法时 参数会自动封装在user类中
    @RequestMapping("/show2")
    public String show(User user, Model model){
        //获取参数
        String name = user.getName();
        String password = user.getPassword();
        int age = user.getAge()==null?0:user.getAge();
        String hobby = user.getHobby();
        //校验值是否符合条件
        ParameterCheck parameterCheck = new ParameterCheck();
        CheckUser checkUser = new CheckUser(name, password, age, hobby);
        List<String> errors = parameterCheck.check(checkUser);
        if (errors.size()!=0){
            //参数不合法
            model.addAttribute("errors",errors);
            return "/index.jsp";
        }
        //验证成功后跳转
        user = new User(1, name, password, age, hobby);
        model.addAttribute("user",user);
        return "/WEB-INF/view/show.jsp";
    }
}

配置文件只要添加扫描包的标签就行 控制器中需要对类和方法使用注解用于映射处理器找到它

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值