web.xml
<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:springmvc-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
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: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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- DispatherServlet ,将请求交给一些特定的 进行处理,就需要咨询一个bean
就是配置处理器的映射,HandlerMapping
作用:将一个url请求,指定给相应的Controller
BeanNameUrlHandlerMapping:这个是默认的,将name,作为url:
-->
<!--对控制器进行注解支持 -->
<context:component-scan base-package="cn.kgc.controller"></context:component-scan>
<mvc:annotation-driven/>
<!-- 完成视图的对应 spring里提供了很多视图解析器
InternalResourceViewResolver: jsp的视图解析器-->
<!-- 对页面路径的一个解析 配置: 前缀 后缀 /WEB-INF/jsp/name.jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
UserController.java
package cn.kgc.controller;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;
@Controller
public class UserController {
private Logger log=Logger.getLogger(UserController.class);
@RequestMapping(value="/in",
method=RequestMethod.GET,
params="username")
public String index1(@RequestParam String username){
System.out.println("今天的天气很好");
log.info("index"+username);
return "index";
}
//将有参变成无参方法,添加:(value="username",required=false)
@RequestMapping(value="/in")
public String indexx(@RequestParam(value="username",required=false) String username){
System.out.println("今天的天气很好xxxxxxx");
log.info("index"+username);
return "index";
}
@RequestMapping(value="/innn")
public ModelAndView index(@RequestParam String username){
System.out.println("今天的天气很好xxxxxxx");
log.info("index"+username);
ModelAndView mv=new ModelAndView();
mv.addObject("username",username);
//设置一个逻辑视图名
mv.setViewName("index");
return mv;
}
}