spring mvc 稍微深入的例子

1、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <filter>
        <filter-name>CharacterFilter</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>CharacterFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

2、根据servlet-name配置的名字,新建一个hello-servlet.xml,和web.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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        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-3.1.xsd">
    
    <context:component-scan base-package="zttc.itat.controller"/>
    <mvc:annotation-driven/>
    <!-- 将静态文件指定到某个特殊的文件夹中统一处理 -->
    <mvc:resources location="/resources/" mapping="/resources/**"/>
    <bean name="/welcome.html" class="zttc.itat.controller.WelcomeController"></bean>
    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!-- 设置multipartResolver才能完成文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000"></property>
    </bean>
</beans>

3、创建一个entity,例如User
package zttc.itat.model;
import org.hibernate.validator.constraints.NotEmpty;
public class User {
    private String username;
    private String password;
    private String nickname;
    private String email;
    
    public User() {
    }
    @NotEmpty(message="用户名不能为空")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    //@Size(min=1,max=10,message="密码的长度应该在1和10之间")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    //@Email(message="邮箱的格式不正确")
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public User(String username, String password, String nickname, String email) {
        super();
        this.username = username;
        this.password = password;
        this.nickname = nickname;
        this.email = email;
    }
    
    public User(String username) {
        super();
        this.username = username;
    }
}

4、创建controller
package zttc.itat.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import zttc.itat.model.User;
@Controller
public class HelloController {
    
    //RequestMapping表示用那个url来对应,这里表示用写hello或直接空,访问这个方法
    @RequestMapping({"/hello","/"})
    public String hello(String username,Model model) {
        System.out.println("hello");
        //使用这个url来访问此链接:http://localhost:8080/springmvc_hello/?username=ddd
        model.addAttribute("username", username);
        System.out.println(username);
        //此时用那个作为key?它默认是使用对象的类型作为key-->model.addAttribute("string",username)
        model.addAttribute(new User("fff"));//就相当于addAttribute("user",new User("fff"));
        model.addAttribute(username);
        return "hello";
    }
    
    @RequestMapping("/welcome")
    public String welcome() {
        return "welcome";
    }
    
}


5、写对应的jsp页面
hello.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>
<body>
<h1>hello ${username }!!</h1>
string:${string }
<!--对象属性的取值方式如下-->
user.username:${user.username }
</body>
</html>

welcom.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>
<body>
<h1>Welcome!!</h1>
</body>
</html>

猜你喜欢

转载自jt120.iteye.com/blog/1922685