Spring MVC(九)常用注解及例子
2017-02-13 16:06
417 查看
1.MyController.java
2.applicationContext.xml
3.first-servlet.xml
4.web.xml
/** * yw * 2016-11-10 */ package com.wb.test01.controller; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import org.apache.commons.fileupload.FileItem; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.commons.CommonsMultipartFile; import org.springframework.web.servlet.ModelAndView; import com.wb.test03.service.UserService; /** * @author yw * 2016-11-10 上午11:09:20 */ @Controller /*@RequestMapping("/test01")*/ /*@RequestMapping(value="/test01",method=RequestMethod.GET)*/ public class MyController { @Autowired private UserService userService; // /** // * @return the userService // */ // public UserService getUserService() // { // return userService; // } // /** // * @param userService the userService to set // */ // public void setUserService(UserService userService) // { // this.userService = userService; // } /*@RequestMapping("/test01")*/ @RequestMapping(value="/test01",method=RequestMethod.POST) public String test01() { return "test01"; } @RequestMapping(value="/a/{path}") public String test02(@PathVariable String path) { System.out.println("test02=====path="+path); return "test02"; } @RequestMapping(value="/b/{path}") public String test03(@PathVariable String path,Model model, @RequestParam(value="username") String name,@RequestParam(value="age") int age) { System.out.println("test03=====path="+path); System.out.println("name="+name); System.out.println("age="+age); model.addAttribute("username",name); model.addAttribute("age",age); return "test03"; } @RequestMapping(value="/c/{path}") public String test04(@PathVariable String path,Model model,@RequestParam(value="username") String name, @RequestParam(value="age",required=false) String age) { System.out.println("test04=====path="+path); System.out.println("name="+name); System.out.println("age="+age); model.addAttribute("username",name); // model.addAttribute("age",age); return "test04"; } @RequestMapping(value="/test05.do") public String test05() { System.out.println("userService="+userService); return "test01"; } @RequestMapping(value="/upload.do",method=RequestMethod.GET) public ModelAndView test06() { return new ModelAndView("upload"); } @RequestMapping(value="/upload.do",method=RequestMethod.POST) public ModelAndView test07(@RequestParam("file") CommonsMultipartFile file,HttpServletRequest request) { System.out.println("request="+request); String path=request.getSession().getServletContext().getRealPath("/"); System.out.println("path="+path); System.out.println("file="+file); FileItem fileItem=file.getFileItem(); System.out.println("fileItem="+fileItem); // File dest=new File("d:/upload/"+file.getFileItem().getName()); File dest=new File(path+"/upload/"+file.getFileItem().getName()); try { file.transferTo(dest); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return new ModelAndView("success"); } }
2.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.wb.test01.controller" /> <!-- 数据源配置 --> <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> </bean> <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:jdbc.properties</value> </property> </bean> <!-- mybatis和spring的整合配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.wb.test03.mapper" /> </bean> <!-- spring的声明式事务配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" autowire="byName"> </bean> <tx:advice id="txAdvice"> <tx:attributes> <tx:method name="*" rollback-for="Exception" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.wb.test03.service.*.*(..))" id="point1" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="point1" /> </aop:config> <!-- 配置Dao和Service --> <bean id="userDao" class="com.wb.test03.dao.impl.UserDaoImpl" autowire="byType"></bean> <bean id="userService" class="com.wb.test03.service.impl.UserServiceImpl" autowire="byType"></bean> </beans>
3.first-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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="com.wb.test01.controller" /> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="100000" /> </bean> <bean id="jspViewResolver" 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> </beans>
4.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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssmday09</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> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>first</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- Java进阶3 —— 类和接口设计原则
- cas单点登录1-源码导入eclipse
- Spring 定时任务之 @Scheduled cron表达式
- BufferedWriter 无法写入文件eclipse提示错误Stream closed
- 同台机器上安装两个jdk版本,想要那个就要那个版本;
- Java后台返回和处理JSon数据的方法
- spring定时器quartz
- 常用JVM配置参数
- [置顶] RxJava中—— —— 变换的使用
- Spring实践(二)AOP的底层实现机制
- JavaBean,List,Map转成json格式
- Leetcode-423. Reconstruct Original Digits from English
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常
- java里需要注意的事(一)
- ActiveMQ学习(二)Java应用
- Java - 集合框架完全解析
- Spring@Autowired详细讲解
- JDK环境变量的配置(Windows平台)
- spring mvc3中的addFlashAttribute方法
- 关于getClass().getClassLoader()