1. 程式人生 > >SpringMVC的註解開發

SpringMVC的註解開發

案例 3.2 path view username component date property xsd

---恢復內容開始---

1.SpringMVC註解開發的案例

配置DispatchServlet-Servlet.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
> <!--1.配置註解的位置--> <context:component-scan base-package="oyb.Controller"></context:component-scan> <!--2.配置處理器映射,通過註解來查找--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!--
3.配置註解處理適配器來執行控制器的方法--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> <!-- 配置springmvc視圖解析器 視圖解析器解析的視頻路徑為:前綴 + 後綴 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views"/> <property name="suffix" value=".jsp"/> </bean> </beans>

添加一個UserController控制器

package oyb.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;

@Controller
public class UserController  {

    @RequestMapping("list")
    public String list(){
        return "/user/userlist";
    }
}

userlist.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用戶列表</title>
</head>
<body>
用戶列表:
${name}<br>

</body>
</html>

別忘了在web.xml添加

<servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

訪問

技術分享圖片

2.接收請求參數

1.通過pojo接收參數

寫一個簡單的頁面

技術分享圖片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>註冊</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/register.do" method="post">
    用戶名:<input type="text" name="username"><br>
    密碼:<input type="text" name="password"><br>
    性別:<input type="text" name="gender"><br>
    年齡:<input type="text" name="age"><br>
    生日:<input type="text" name="birthday"><br>
    愛好:<input type="checkbox" name="hobbyIds" value="1">打球
    <input type="checkbox" name="hobbyIds" value="2">打代碼
    <input type="checkbox" name="hobbyIds" value="3">攝影<br>
    <input type="submit">
</form>

<br>

</body>
</html>

寫一個持久類pojo

package oyb.model;
import java.util.Arrays;
import java.util.Date;

public class User {
    private Integer id;

    private String username;
    private String password;
    private  int age;
    private String gender;
    private Date birthday;
    private  String[] hobbyIds;

    public User(String username, int age, String gender, Date birthday) {
        this.username = username;
        this.age = age;
        this.gender = gender;
        this.birthday = birthday;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public User() {
    }

    @Override
    public String toString() {
        return "User{" +
                "username=‘" + username + ‘\‘‘ +
                ", password=‘" + password + ‘\‘‘ +
                ", age=" + age +
                ", gender=‘" + gender + ‘\‘‘ +
                ", birthday=" + birthday +
                ", hobbyIds=" + Arrays.toString(hobbyIds) +
                ‘}‘;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String[] getHobbyIds() {
        return hobbyIds;
    }

    public void setHobbyIds(String[] hobbyIds) {
        this.hobbyIds = hobbyIds;
    }
}

在controller中添加

@RequestMapping("register")
    public String register(User user){
        System.out.println(user);

        return "/user/info";
    }

測試

技術分享圖片

技術分享圖片

2.接收包裝類類型

寫一個包裝類

package oyb.model;

public class UserExt {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String toString() {
        return "UserExt{" +
                "user=" + user +
                ‘}‘;
    }
}

提交的表單寫法如下:

<form action="${pageContext.request.contextPath}/register2.do" method="post">
    用戶名:<input type="text" name="user.username"><br>
    密碼:<input type="text" name="user.password"><br>
    性別:<input type="text" name="user.gender"><br>
    年齡:<input type="text" name="user.age"><br>
    生日:<input type="text" name="user.birthday"><br>
    愛好:<input type="checkbox" name="user.hobbyIds" value="1">打球
    <input type="checkbox" name="hobbyIds" value="2">打代碼
    <input type="checkbox" name="hobbyIds" value="3">攝影<br>
    <input type="submit">
</form>

測試

技術分享圖片

技術分享圖片

SpringMVC的註解開發