学习Spring框架有一两个礼拜了,在这之前JAVA SSH的基础几乎没有,一直从事WPF的那一套开发,之前做的网页比较小用javaBean+servlet+jsp自己都能解决,但是跨到javaWeb学习之后还是有点教训和经验,我还没到大牛的层次,在应用层面上来说,C#和JavaWeb的开发的差别还是有点大的。废话说少点,最好在学Spring之前有struts的一些常识,不然以些bean的概念理解起来有些头疼。言归正转:
SpringMVC基本上有三种模式:基于XML,基于注解,基于java类;第一种,简单的来说就是利用反射来装载配置文件,第二种,就是注解后自动加载,大大节省人力,第三种,类似第二种,更加灵活,我还不是很熟悉;不过一般开发,都是基于注解的模式。
在这里我有些话说,有编程基础的,可能直接照着例子ctrl+c和ctrl+v注解模式感觉很简单,也很容易,只要完成就行了,如果你的任务是这个,那恭喜你,你成功了!我学习的经验是,因为是按照先后顺序发展的,如果你直接学注解,可能你就是知其然不知其所以然了,所以,还是得从基础的xml配置学起,一步一步的成为大牛.
首先还是配置Spring的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"> <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/dispatcherServlet-servlet.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>再接着就是配置web.xml中提到的dispatcherServlet-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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 定义映射 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="helloWorld.do">helloWorldAction</prop> </props> </property> </bean> <!-- 定义视图及jsp存放路径 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- 定义控制器 --> <bean id="helloWorldAction" class="com.gc.action.HelloWorldAction"> <property name="methodNameResolver"> <ref bean="paraMethodResolver"/> </property> <property name="viewPage"> <value>show</value> </property> </bean> <!-- 定义动作参数 --> <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName"> <value>method</value> </property> </bean> </beans>
package com.gc.action; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestUtils; import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; public class HelloWorldAction extends MultiActionController { private Logger logger = Logger.getLogger(this.getClass().getName()); private String viewPage; public ModelAndView insert(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "insert:" + helloWorld); return new ModelAndView(getViewPage(), model); } public ModelAndView delete(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "delete:" + helloWorld); return new ModelAndView(getViewPage(), model); } public ModelAndView update(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "update:" + helloWorld); return new ModelAndView(getViewPage(), model); } public String getViewPage() { return viewPage; } public void setViewPage(String viewPage) { this.viewPage = viewPage; } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="helloWorld.do" method="post"> Welcome<input type="text" name="msg" value=""/> <input type="submit" name="method" value="insert" /> <input type="submit" name="method" value="update" /> <input type="submit" name="method" value="delete" /> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String str = (String)request.getAttribute("helloWorld"); %> <body> <form action="/8_4/helloWorld.do" name="HelloWorld" method="post"> you Enter is :${helloWorld }<br> </form> </body> </body> </html>
入口:
结果页显示:
还有一种方案,即不继承MultiActionController,在配置文件中完成。
HelloWorldAction.java修改如下:
package com.gc.action; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.ServletRequestBindingException; import org.springframework.web.bind.ServletRequestUtils; import org.apache.log4j.Logger; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.multiaction.MultiActionController; //public class HelloWorldAction extends MultiActionController { public class HelloWorldAction { private Logger logger = Logger.getLogger(this.getClass().getName()); private String viewPage; public ModelAndView insert(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "insert:" + helloWorld); return new ModelAndView(getViewPage(), model); } public ModelAndView delete(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "delete:" + helloWorld); return new ModelAndView(getViewPage(), model); } public ModelAndView update(HttpServletRequest req, HttpServletResponse res) throws ServletRequestBindingException { String helloWorld = ServletRequestUtils.getRequiredStringParameter(req, "msg"); Map model = new HashMap(); model.put("helloWorld", "update:" + helloWorld); return new ModelAndView(getViewPage(), model); } public String getViewPage() { return viewPage; } public void setViewPage(String viewPage) { this.viewPage = viewPage; } }
<?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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 定义映射 --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="helloWorld.do">action</prop> </props> </property> </bean> <!-- 定义视图及jsp存放路径 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView</value> </property> <property name="prefix"> <value>/WEB-INF/pages/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> <!-- 定义控制器 --> <bean id="helloWorldAction" class="com.gc.action.HelloWorldAction"> <!-- <property name="methodNameResolver"> <ref bean="paraMethodResolver"/> </property> --> <property name="viewPage"> <value>show</value> </property> </bean> <!-- 定义动作参数 --> <bean id="paraMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver"> <property name="paramName"> <value>method</value> </property> </bean> <!-- 使用多控制器代理类 --> <bean id="action" class="org.springframework.web.servlet.mvc.multiaction.MultiActionController"> <property name="methodNameResolver"> <ref bean="paraMethodResolver"/> </property> <property name="delegate"> <ref bean="helloWorldAction"/> </property> </bean> </beans>