Spring mvc (八) [基于注解的案例][formbean的传递以及map传递参数]

下面结合一个案例来解析一个spring mvc通过注解的方式如何运作的:
1、   发送请求的url:
http://localhost:8080/@mvc/do_get.do发送请求道do_get方法。通过注解实现的。
2、   配置请求的通过xml文件:
Web.xml
<?xml version="1.0"encoding="UTF-8"?>
<web-app version="2.5"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 
  <servlet>       
          <servlet-name>mvc</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
          <!--查找controller的xml文件配置-->
          <init-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>/WEB-INF/mvc.xml</param-value>
          </init-param>
          <load-on-startup>1</load-on-startup>
         
  </servlet>
  <servlet-mapping>
              <servlet-name>mvc</servlet-name>     
              <url-pattern>*.do</url-pattern> 
  </servlet-mapping>
  </web-app>
配置bean的xml文件。mvc.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"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-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
     ">
    
     <!--主动扫描Controller的配置-->
     <context:component-scan base-package="cn.zhang.mvc" />
    
    
     <!--配置一个返回视图用的bean,下面的这个class整合了jstl技术-->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
         <property name="prefix"value="/WEB-INF/pages/"></property>
         <property name="suffix"value=".jsp"></property>
     </bean>
</beans>
 
3、   Controller的编写
@Controller
public class UserAction {
     @RequestMapping(value = "/do_get" , method = RequestMethod.GET)
     public String do_get(){
         System.out.println("this isdoget method");
         return "login";
     }
    
     @RequestMapping(value = "/do_post" , method = RequestMethod.POST)
     public void do_post(@ModelAttribute User user,Map model){
         //如果想在这个方法执行完毕之后传递对象那么就传递一个map对象。然后将map
         //对象put一些obj。然后再return的时候就会自动return
         System.out.println(user);
         System.out.println("this isdopost method");
     }
}
4、   FormBean的编写
public class User {
     private String username;
     private String password;
    
     public String getUsername() {
         return username;
     }
     public void setUsername(String username) {
         this.username = username;
     }
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         this.password = password;
     }
     @Override
     public String toString() {
         return username+password;
     }
}
 
5、    中间过渡页即发送post请求的页:
<body>
          <form action="do_post.do" method="post">
              用户名:<input type="text" name="username"> <br/>
              密码 : <input type="password" name="password">
              <input type="submit" value="提交"/>
          </form>
  </body>


你可能感兴趣的:(spring,bean,mvc,String,Class,encoding)