SpringMVC(简单入门)

来源:互联网 发布:dll编程 .def 编辑:程序博客网 时间:2024/05/29 07:17

总结一下最近学习的SpringMVC的知识

  • web.xml与SpringMVC配置文件
  • 一些基本的注解

<?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_3_1.xsd"         version="3.1">    <servlet>        <servlet-name>dispatcher</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath:applicationContext.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcher</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

在web.xml文件中配置servlet:org.springframework.web.servlet.DispatcherServlet
是SpringMVC为我们提供的,在这里拦截了所有的请求。
applicationContext.xml文件则是我们的SpringMVC的配置文件。

<?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"       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">        <bean name="Product" class="app01.Product"/>        <!-- 配置自动扫描的包-->        <context:component-scan base-package="app01.main"/>        <!-- 配置视图请求-->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">                <property name="prefix" value="/WEB-INF/views/"/>                <property name="suffix" value=".jsp" />        </bean></beans>

SpringMVC配置文件中添加:xmlns:context=”http://www.springframework.org/schema/context”
引入这个才能调用:
扫描我们存放Controller的包。


@Controller类

Controller类是我们的控制器,简单的说,就是处理我们求情的地方。

@RequestMapping(value = “/xxxx”)

@RequestMapping,这个注解主要用来对应的URL请求。
如果其中只有URI一个参数的话,可以不用写value(默认参数),如果有多个参数则必须写出来如
@RequestMapping(value=”/xxxx”,method =”RequestMethod.POST”)。
@RequestMapping(value=”/xxxx”,method ={“RequestMethod.POST”,”RequestMethod.PUT”})。

@Controllerpublic class Test{    @RequestMapping("/xxx")    public String method01(){        //do some thing...        return "ooooo";    }}

@RequestMapping(…)
也可以用来修饰类

@Controller@RequestMapping("/book")public class Test{    @RequestMapping("/EnglishBook")    public String method01(){        //do some thing...        return "ooooo";    }}//这里访问的路径则是 /book/EnglishBook

处理参数

调用servlet API

@RequestMapping("/uri")public String method(HttpSession session){    session.setAttribute(key,value);    ...}//HttpRequest 当然也行。。。

接收参数

@Controllerpublic class Test {    @RequestMapping(value="/helloworld" parms="username","age")    public String Hello(){        System.out.println("hello world");        return"success";    }}

使用@RequestParm绑定请求参数

@RequestMapping("/testparm")public String Test(@RequestParm(value="username") String un,@RequestParm(value="age",required=false) Integer age){        System.out.println("username:"+un);}//required 设置是否是一定要带的参数

POJO对象绑定请求参数值

假定一个USER对象
private String name;
private String password;
private Address address; //级联属性

假定一个Address对象
private String provice;
private String city;

假定一个form表单

<form action="/test" method="post">    name:<input type="text" name="name"/>    password:<input type ="password" name="password">    city:<input type="text" name="address.city/>    privice:<input type="text" name="address.name"/></form>
@RequestMapping("/test")public String method(User user){    //可以直接接收到user对象啦。。。}

@Autowired和@Service进行依赖注入

SpringMVC毕竟属于Spring的一部分,Spring框架的好处就是容易进行依赖注入。

如果需要类能被作为依赖注入,类必须注明为@Service。Service注释类型指示类是一个服务。另外需要在SpringMVC配置文件中需要添加一个:

<context: component-scan base-pakage="dependencyPackge">
public class product{    @Autowired    private productService service;}@Servicepublic class productService{    ...}

先记录这些吧~

原创粉丝点击