A-A+

spring mvc(注解方式)+freemarker框架的搭建

2014年07月19日 Spring mvc 评论 1 条 阅读 484 views 次

实现效果:从jsp页面传递一个uname到controller层,在controller层处理后绑定到ModelAndView中,然后通过freemarker模板输出到html中

项目文件结构

QQ截图20140719190543

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" version="2.5">
  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/springmvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</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>*.do</url-pattern>
  </filter-mapping>
</web-app>

springmvc-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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     
    <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
    <context:component-scan base-package="com.sxt"/>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

    <!--对模型视图名称的解析,即在模型视图名称添加前后缀 (使用freemarker的时候要注意这个设置会给view加上后缀,导致访问不到feemarker的模板)-->
<!--     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"  -->
<!--         p:suffix=".jsp"/> -->
        
    <!-- freemarker的配置 -->  
    <bean id="freemarkerConfigurer"  
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">  
        <property name="templateLoaderPath" value="/WEB-INF/view/" />  
        <property name="defaultEncoding" value="utf-8" />  
        <property name="freemarkerSettings">  
            <props>  
                <prop key="template_update_delay">10</prop>  
                <prop key="locale">zh_CN</prop>  
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>  
                <prop key="date_format">yyyy-MM-dd</prop>  
                <prop key="number_format">#.##</prop>  
            </props>  
        </property>  
    </bean>  
    <!-- FreeMarker视图解析 如返回userinfo。。在这里配置后缀名ftl和视图解析器。。 -->  
    <bean id="viewResolver"  
        class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">  
        <property name="viewClass"  
            value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />  
        <property name="suffix" value=".ftl" />  
        <property name="contentType" value="text/html;charset=utf-8" />  
        <property name="exposeRequestAttributes" value="true" />  
        <property name="exposeSessionAttributes" value="true" />  
        <property name="exposeSpringMacroHelpers" value="true" />  
    </bean>  
 
    <bean id="messageSource"  
        class="org.springframework.context.support.ResourceBundleMessageSource"  
        p:basename="i18n/messages" />
</beans>

UserController.java


package com.sxt.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.sxt.po.User;
import com.sxt.service.UserService;

@Controller
@RequestMapping("/user.do")
public class UserController{
    @Autowired
    private UserService userService;
    
    @RequestMapping(params = "method=user")
    public ModelAndView user(User u,@RequestParam(value="uname")String uname,ModelMap modelMap){
        ModelAndView mav=new ModelAndView();  
        mav.addObject("uname",uname);
        mav.setViewName("index");
        return mav;
    }

}

User.java


package com.sxt.po;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
	private String uname;

	public int getId() {
		return id;
	}

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

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

}

UserDao.java(非必须,只是为了方便理解mvc框架)


package com.sxt.dao;

import org.springframework.stereotype.Repository;

import com.sxt.po.User;

@Repository
public class UserDao {
	public void add(User u) {
		System.out.println("UserDao.add()");
	}
}

UserService.java(非必须,只为方便理解mvc框架)


package com.sxt.service;

import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.sxt.dao.UserDao;
import com.sxt.po.User;

@Service
public class UserService {
	@Autowired
	private UserDao userDao;

	public void add(String uname) {
		System.out.println("UserService.add()");
		User u = new User();
		u.setUname(uname);
		userDao.add(u);
	}
}

工程实例下载:http://pan.baidu.com/s/1i3oUcqp

标签:
admin

1 条留言  访客:1 条  博主:0 条

  1. fandy

    不行,点击开始上传之后,就没反应了

给我留言

Copyright © 大师级 保留所有权利.   Theme  大师级 桂ICP备14003638号

用户登录

分享到: