您的位置:首页 > 编程语言 > Java开发

SpringMVC学习笔记(一)

2016-02-05 13:42 435 查看

// 搭建最基本的Spring MVC框架

// 1.导入相应的包,附件案例中有相应的包

// 2.编写spring-servlet.xml配置文件,这个文件放在同web.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"
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">
<!-- 指定扫描的包 -->
<context:component-scan base-package="com.cc.testSpringMVC.web" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

 // 3.配置web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>TestSpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<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>/</url-pattern>
</servlet-mapping>
</web-app>

 

 

// 4.编写相应的控制器,目录就是spring-servlet.xml配置中,扫描的包

@Controller
public class TController {

// 只接收post请求,如果不写,则都可以
@RequestMapping(value="index",method=RequestMethod.POST)
public ModelAndView index(HttpServletRequest request) {
String str = "Spring MVC";

// 返回指定的jsp页面
ModelAndView mav = new ModelAndView("message");
// 添加参数
mav.addObject("str",str);

return mav;
}

}

 // 5.最后编写相应的jsp文件index.jsp与success.jsp

  <a href="index.do">Spring MVC 示例</a>

 

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: