首页 > 代码库 > Spring系列【12】简单MVC应用

Spring系列【12】简单MVC应用

User.java

技术分享
 1 package com.user; 2  3 public class User { 4     private String username; 5     private String password; 6     public String getUsername() { 7         return username; 8     } 9     public void setUsername(String username) {10         this.username = username;11     }12     public String getPassword() {13         return password;14     }15     public void setPassword(String password) {16         this.password = password;17     }18 }
View Code

Validation.java

 1 package com.validation; 2  3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5  6 import org.springframework.web.servlet.ModelAndView; 7 import org.springframework.web.servlet.mvc.AbstractController; 8  9 import com.user.User;10 11 public class Validation extends AbstractController {12     private User user;13 14     public User getUser() {15         return user;16     }17 18     public void setUser(User user) {19         this.user = user;20     }21     protected ModelAndView handleRequestInternal(HttpServletRequest request,22             HttpServletResponse response) throws Exception {23         String username=request.getParameter("username");24         String password=request.getParameter("password");25     if(username.equals(user.getUsername())&&password.equals(user.getPassword())){26             return new ModelAndView("yes");27         }else{28             return new ModelAndView("error");29         }30     }31 }

技术分享

以上为本例要用到的jar包,与前几例相比,多了web 和webmvc两个包。

Spring的配置文件如下:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">    <!--定义控制器转发视图类 -->    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <value>/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean>    <!-- 为User对象属性赋值 -->    <bean id="user" class="com.user.User">        <property name="username">            <value>admin</value>        </property>        <property name="password">            <value>111</value>        </property>    </bean>    <!-- 让bean Validation自动装配User并映射do -->    <bean name="/login.do"  autowire="byName" class="com.validation.Validation">        <property name="user" ref="user">        </property>    </bean></beans>

web.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">  <display-name>zidingyi</display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <servlet>        <servlet-name>dispatcherServlet</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/applicationContext.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>dispatcherServlet</servlet-name>        <url-pattern>*.do</url-pattern>    </servlet-mapping></web-app>

文件结构如下:

技术分享

注意文件名与return new ModelAndView("yes");的对应关系。。。

 

特别留言:与User类成员变量的对应关系

技术分享

Spring系列【12】简单MVC应用