SpringMVC作为一个MVC框架功能较struts2更加灵活,要写一个SpringMVC程序需要做到:
第一步:添加SpringMVC所需的jar包,主要包括
commons-logging-1.1.1.jar
javassist-3.15.0-GA.jar
org.springframework.asm-3.1.1.RELEASE.jar
org.springframework.beans-3.1.1.RELEASE.jar
org.springframework.context-3.1.1.RELEASE.jar
org.springframework.core-3.1.1.RELEASE.jar
org.springframework.expression-3.1.1.RELEASE.jar
org.springframework.web-3.1.1.RELEASE.jar
org.springframework.web.servlet-3.1.1.RELEASE.jar
第二步:在web.xml文件中配置SpringMVC核心控制器
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
第三步:配置[servlet-name]-servlet.xml文件(WEB-INF下,此路径可以通过配置指定到任意位置,此处不赘述)
<?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">
<!--表示这里使用注解的方式开发 -->
<mvc:annotation-driven/>
<!-- xmlns:mvc="http://www.springframework.org/schema/mvc" 命名空间表示使用注解方式 -->
<bean id="myController" class="com.spinfo.controller.LoginController"></bean>
<!-- URLMapping(用来寻找请求对应的Controller) -->
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello.do">myController</prop>
<prop key="/login.do">myController</prop>
</props>
</property>
</bean>
<!-- jsp页面解析器,当Controller返回XXX字符串时,就会在WEB-INF/jsp下寻找XXX.jsp文件-->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第四步:写java代码
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.spinfo.entity.User;
@Controller
public class LoginController{
@RequestMapping("/hello")
public String handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception
{
System.out.println("运行springmvc2--Controller");
return "login";
}
@RequestMapping("/login")
public ModelAndView login(String username,String password){
System.out.println("usrename:"+username+" / password:"+password);
String message = "helloSpringMVC";
return new ModelAndView("success","message",message);
}
简单的几个页面跳转,如果使用spring整合配置会跟简单,下次再贴.