java学习-springmvc

一.简单的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">
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个SpringMVC 的配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:SpringMvc-servlet.xml</param-value>
        </init-param>
        <!-- 服务器启动的时候就启动 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--
        /  匹配所有的请求,不包括 .jsp
        /*  匹配所有的请求,包括 .jsp
    -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

然后配置SpringMvc-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:component-scan base-package="com.gg.controller"/>
    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>

    </bean>


</beans>

写Hellocontroller

@Controller
public class Hellocontroller {

    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("h","你好");
        System.out.println("aaaaa");
        return "hello";
    }
}

hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${h}
</body>
</html>

二,springmvc的controller本身方法返回就是1.转发,2.外加重定向(hello1),3.还有使用对象进行多个的参数接受(hello2)
下面的完整的例子
在这里插入图片描述

User

public class User {
    private int id;
    private String name;
    private String pwd;
}
@Controller

public class Hellocontroller {
    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("h","你好");
        System.out.println("aaaaa");
        return "hello";
    }

    @RequestMapping("/hello1")
    public String hello1(Model model){
        model.addAttribute("h","你好");
        System.out.println("aaaaa");
        return "redirect:/hello";
    }

    @RequestMapping("/hello2")
    public String hello2(Model model, User user){
        model.addAttribute("h","你好");

        System.out.println("aaaaa");
        return "redirect:/hello";
    }
}

使用form表单;
index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <form action="/login" method="post">
    用户名:<input type="text" name="username">
    密码:<input type="text" name="password">
    <input type="submit">
  </form>
  </body>
</html>

web.xml,再其中配置加上spring的过滤器的内容,过滤乱码

<?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">

    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>F</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>F</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

LoginController,前端打印用户名,并且转发到hello.jsp

@Controller
public class LoginController {
    @RequestMapping("/login")
    public String login(String username, String password, Model model){
        model.addAttribute("msg",username);
        return "hello";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值