java代码
1 package com.tgb.web.controller.annotation; 2 3 import javax.servlet.http.HttpServletRequest; 4 5 import org.springframework.stereotype.Controller; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 8 @Controller 9 @RequestMapping("/user2") 10 public class User2Controller { 11 12 @RequestMapping("/addUser") 13 public String addUser(HttpServletRequest request){ 14 15 String result ="this is addUser------优化版"; 16 request.setAttribute("result", result); 17 return "/jquery"; 18 } 19 20 @RequestMapping("/delUser") 21 public String delUser(HttpServletRequest request){ 22 String result ="this is delUser------优化版"; 23 request.setAttribute("result", result); 24 return "/jquery"; 25 } 26 @RequestMapping("/toUser") 27 public String toUser(HttpServletRequest request){ 28 return "/jquery"; 29 } 30 }
配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:mvc="http://www.springframework.org/schema/mvc" 6 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/mvc 12 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 13 <!-- 注解扫描包 --> 14 <context:component-scan base-package="com.tgb.web.controller.annotation" /> 15 <!-- 开启注解 --> 16 17 <mvc:annotation-driven/> 18 19 <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> 20 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> --> 21 <!-- 静态资源访问 --> 22 <mvc:resources location="/img/" mapping="/img/**"/> 23 <mvc:resources location="/js/" mapping="/js/**"/> 24 25 26 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 27 <property name="prefix" value="/"></property> 28 <property name="suffix" value=".jsp"></property> 29 </bean> 30 </beans>
时间: 2024-10-06 05:42:52