代码改变世界

spring-mvc-01

2018-12-01 21:15  crow!  阅读(212)  评论(0编辑  收藏  举报

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>springIoc</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <listener>
          <listener-class>
              org.springframework.web.context.ContextLoaderListener
          </listener-class>
  </listener>
       <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <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:applicationContext.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>

ApplicationContext.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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:security="http://www.springframework.org/schema/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">
    
    <context:annotation-config />
    <context:component-scan base-package="cn.mldn" />
    
    <!-- 配置Spring MVC的相关操作    -->
    <mvc:annotation-driven/>
    <!-- 配置默认的servlet程序处理    -->
    <mvc:default-servlet-handler/>
   
</beans>

通过这两步 mvc的换进配置好了

第一个mvc程序如下

package cn.mldn.action;

import java.util.Date;
import java.text.SimpleDateFormat;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.mldn.vo.Emp;

@Controller
@RequestMapping("/pages/back/emp/*")    //此处描述的是定义父路劲
public class EmpAction {  //完全独立的类
    
    //ModelAndView 设置跳转路径和传递数据的类
    //@RequestMapping(value="empAdd",method=RequestMethod.POST) 
    @RequestMapping(value="empAdd") 
    public ModelAndView add(Emp emp) {  //此处表接收的参数就是Emp类型
        System.out.println(emp);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/forward.jsp"); //设置跳转路径
        mav.addObject("msg" , "天要下雨");
        mav.addObject("myemp", emp);
        return mav ;
    }

@RequestMapping(value="empEdit")
public ModelAndView edit (
@RequestParam(value="eid",defaultValue="10") int empno) {
//这里的参数还可以传 request ,response
System.out.println(empno);
return null;
}

    @InitBinder
    public void initBinder(WebDataBinder binder) {    //转换器
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 将我们自定一的转换编辑器进行配置,表i是以后发现有Date类型,就使用sdf对象进行转换,并且允许数据为null;
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}

测试的url:http://localhost/springIoc/pages/back/emp/empAdd.action?empno=7369&ename=smith&dept.deptno=20&dept.dname=%E9%94%80%E5%94%AE%E9%83%A8&hiredate=2001-01-01%2011:11:11

 

package cn.mldn.vo;

import java.io.Serializable;
import java.util.Date;

public class Emp implements Serializable {
    private Integer empno ;
    private String  ename ;
    private Date    hiredate ;
    private Double dal ;
    private Dept   dept ;
    
    public Integer getEmpno() {
        return empno;
    }
    public void setEmpno(Integer empno) {
        this.empno = empno;
    }
    public String getEname() {
        return ename;
    }
    public void setEname(String ename) {
        this.ename = ename;
    }
    public Date getHiredate() {
        return hiredate;
    }
    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }
    public Double getDal() {
        return dal;
    }
    public void setDal(Double dal) {
        this.dal = dal;
    }
    public Dept getDept() {
        return dept;
    }
    public void setDept(Dept dept) {
        this.dept = dept;
    }
    
    @Override
    public String toString() {
        return "Emp [empno=" + empno + ", ename=" + ename + ", hiredate=" + hiredate + ", dal=" + dal + ", dept=" + dept
                + "]";
    }
    
}
package cn.mldn.vo;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Dept implements Serializable {
    private Integer deptno ;
    private String  dname ;
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    @Override
    public String toString() {
        return "Dept [deptno=" + deptno + ", dname=" + dname + "]";
    }

}

 

配置安全访问!!!!!!!!!!!!!!!!!!!

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp" />
    </bean>
mav.setViewName("forward"); //设置跳转路径

这样就可以访问

/WEB-INF/pages/下的jsp了

资源文件的读取
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <array>
                <value>Messages</value>
            </array>
        </property>
    </bean>
    @Resource
    private MessageSource msgSource ; //自动匹配注入
    
    @RequestMapping(value="xxx")
    public ModelAndView test() {
        
        System.out.println(msgSource.getMessage("welcome.url", null, Locale.getDefault()));
        return null;
    }