spring mvc

来源:互联网 发布:看舌苔知病情图解 编辑:程序博客网 时间:2024/05/31 00:40

先加入SpringMVC的jar包,这个官网上有,下载下来放到lib文件夹下。

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>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>/</url-pattern><!-- 意思是拦截所有请求 -->
  </servlet-mapping>
</web-app>


springMVC-servlet.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 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-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
                    ">
    <bean name="/test/helloworld" class="com.mcm.web.controller.HelloWorldController"></bean>
 <!-- 配置视图解析器 -->
 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
     <property name="prefix" value="/"/>
     <property name="suffix" value=".jsp"/>
 </bean>                  
 </beans>   


HelloWorldController.java文件:

package com.mcm.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class HelloWorldController implements Controller {

 public ModelAndView handleRequest(HttpServletRequest arg0,
   HttpServletResponse arg1) throws Exception {
  System.out.println("Hello my SpringMVC!");
  return new ModelAndView("/aaa");
 }

}

aaa.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>My JSP 'aaa.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">

  </head>
  <body>
    我的第一个SpringMVC例子<br>
  </body>
</html>

项目的文件目录如图所示:

 




2、

首先来看登录界面

对应的index.html:

<html>

<body>

<form method="POST" action="login.do">

<p align="center">登录</p>

<br>

用户名:

<input type="text" name="username" >

<br>

密 码 :

<input type="password" name="password" >

<br>

<p>

<input type="submit" value="提交name="B1">

<input type="reset" value="重置name="B2">

</p>

</form>

</body>

</html>



此模式在web.xml中设定:

<?xml version="1.0" encoding="ISO-8859-1"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"

version="2.4">

<servlet> 

<servlet-name>Dispatcher</servlet-name>

<servlet-class>

org.springframework.web.servlet.DispatcherServlet

</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/Config.xml</param-value>

</init-param>

</servlet>

<servlet-mapping> 

<servlet-name>Dispatcher</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

</web-app>


我们在<init-param>节点中配置了名为“contextConfigLocation”的

Servlet参数,此参数指定了Spring配置文件的位置“/WEB-INF/Config.xml”。

如果忽略此设定,则默认为“/WEB-INF/<servlet name>-servlet.xml”,

<servlet name>Servlet 名替换(在当前环境下,默认值也就是

/WEB-INF/Dispatcher-servlet.xml


Config.xml,此文件包含了所有的“请求/处理单元”关系映射设定,以及返回

时表现层的一些属性设置。

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

"http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<!--Definition of View Resolver -->

<bean id="viewResolver" 

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"

<value>

org.springframework.web.servlet.view.JstlView

</value>

</property>

<property name="prefix"

<value>

/WEB-INF/view/

</value>

</property>

<property name="suffix"

<value>.jsp</value>

</property>

</bean>

<!--Request Mapping -->

<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

<property name="mappings">

<props>

<prop key="/login.do">LoginAction</prop>

</props>

</property>

</bean>

<!---Action Definition-->

<bean id="LoginAction" class="net.xiaxin.action.LoginAction">

<property name="commandClass"

<value>net.xiaxin.action.LoginInfo</value>

</property>

<property name="fail_view"

<value>loginfail</value>

</property>

<property name="success_view">

<value>main</value>

</property>

</bean>

</beans>



这里我们指定commandClass net.xiaxin.action.LoginInfo,这是一个非

常简单的Java Bean,它封装了登录请求所需的数据内容:

public class LoginInfo {

private String username;

private String password;

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

}

Spring会根据LoginActioncommandClass定义自动加载对应的LoginInfo

实例。



⑻ 返回视图定义

对于这里的LoginAction 而言,有两种返回结果,即登录失败时返回错误界面,登

录成功时进入系统主界面。

对应我们配置了fail_viewsuccess_view两个自定义参数。

参数值将由Resolver进行处理,为其加上前缀后缀,如对于fail_view而言,实

际的视图路径为/WEB-INF/view/loginfail.jsp

之后,Resolver 会将LoginAction的返回数据与视图相融合,返回最终的显示界

面。


LoginAction.java

public class LoginAction extends SimpleFormController {

private String fail_view;

private String success_view;

protected ModelAndView onSubmit(Object cmd,BindException ex)throws Exception {

LoginInfo loginInfo = (LoginInfo) cmd;

HashMap result_map = new HashMap();

if (login(loginInfo) == 0) {

result_map.put("logininfo", loginInfo);

List msgList = new LinkedList();

msgList.add("msg1");

msgList.add("msg2");

msgList.add("msg3");

result_map.put("messages", msgList);

return new  ModelAndView(this.getSuccess_view(), result_map); 

} else {

result_map.put("failmsg", new String("Sorry, you input the wrong username or password!"));

return new ModelAndView(this.getFail_view(), result_map);

}

}

private int login(LoginInfo loginInfo) {

if ("Erica".equalsIgnoreCase(loginInfo.getUsername())

&& "mypass".equals(loginInfo.getPassword())) {

return 0;

}

return 1;

}

public String getFail_view() {

return fail_view;

}

public String getSuccess_view() {

return success_view;

}

public void setFail_view(String string) {

fail_view = string;

}

public void setSuccess_view(String string) {

success_view = string;

}

}



<html>

<body>

<p>Login Success!!!</p>

<p>Current User:

<c:out value="${logininfo.username}"/><br>

</p>

</html>

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%>

<html>

<body>

<p>Login Fail!!!</p><c:out value="${failmsg}" />

</body>

</html>



0 0
原创粉丝点击