springMVC怎么可以访问.do和.jsp

结果:访问.do报404错误;图片说明

访问.jsp可以正常访问:
图片说明

项目结构:
图片说明

两个Controller:
图片说明
这个返回值是json格式的。

图片说明

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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>springmvc</display-name>

    <!-- Spring配置 -->
     <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean.xml</param-value>
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- DispatcherServlet配置 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>




    <!-- 为了能够处理中文的post请求,再配置一个encodingFilter,以避免post请求中文出现乱码情况 -->
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

springmvc.xml

    <context:component-scan
        base-package="com.web"></context:component-scan>

    <!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->

    <!-- 静态资源(js、image等)的访问 -->
    <mvc:default-servlet-handler />

    <!-- 开启注解 -->
    <mvc:annotation-driven />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>

bean.xml

    <!-- 1. 数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        省略啦!!!!
    </bean>


    <!-- 2. Spring 对SessionFactory创建支持 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingLocations">
            <list>
                <value>classpath:com.pojo/*.hbm.xml</value>
            </list>
        </property>
    </bean>

    <!-- 3. 事务管理 -->
    <!-- a. 配置事务管理器类(切面类) -->
    <bean id="txManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- b. 事务通知 -->

    <tx:advice id="txAdvice" transaction-manager="txManager">

    </tx:advice>

    <!-- c. Aop配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* *Service.*(..))"
            id="pt" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
    </aop:config>

    <!--  公用的配置 
    abstract="true" 表示是一个抽象的bean,不会被实例化; 可以被其他bean继承,就可以省略父bean定义多的属性 -->
    <bean id="baseDao" abstract="true">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- dao配置 -->
    <bean id="userDao" parent="baseDao" class="com.dao.UserDao"></bean>

    <!-- service配置 -->
    <bean id="userService" class="com.service.UserService">
        <property name="userDao" ref="userDao"></property>
    </bean>

    <!-- controller配置 -->
    <bean id="userController" class="com.controller.UserController">
        <property name="userService" ref="userService"></property>
    </bean>
</beans>

请问怎么可以访问.do和.jsp呢?

2个回答

吧@RequestMapping注解中value中得.do去掉,再访问就行了。

Lei_Da_Gou
Lei_Da_Gou 空指针啊,报错信息里面看哪行报空,对应解决就行了
9 个月之前 回复
rijyakurinn
rijyakurinn 可以了!!但是访问的时候一直报Servlet.service() for servlet [springDispatcherServlet] in context with path [/springmvc] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException这样的错...调用和service相关的方法就会报这样的错...您知道是怎么回事吗?
9 个月之前 回复

你的项目改过名字吗?项目右键-properties ,弹框出来之后搜索web,看看web 的项目名称是什么

rijyakurinn
rijyakurinn 没有....按照第一个评论改,就可以访问了的。
9 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
请问如何让springmvc支持.jsp后缀访问后台方法
如题, 如果直接在web.xml加 ``` <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping> ``` 这样写访问浏览器直接提示无法重定向
springMVC中jsp页面访问路径404错误?
配置如下 spring-servlet.xml <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="cache" value="true" /> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> Web.xml <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 我在JSP页面的路径是WebContent/views/EnterpriseInfoManag/pollutionBasInfo.jsp controller的代码是: @Controller public class PollutionBasInfoController { @Resource private PollutionBasInfoService pollutionBasInfoService; @RequestMapping("/findPollutionBasInfo.do") public @ResponseBody List<TBasEnterprise> findPollutionBasInfo(){ List<TBasEnterprise> list = pollutionBasInfoService.findPollutionBasInfo(); return list; } 我用JS访问controller: url:'findPollutionBasInfo.do' 但是地址就自动变成 http://localhost:8080/SKPWMS/views/EnterpriseInfoManag/findPollutionBasInfo.do ,访问controller的路径自动在前面加上了/views/EnterpriseInfoManag,导致404错误,求指导,为什么会这样,应该怎样解决啊???
springMVC无法访问controller,这是怎么回事啊
spring配置 ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 开启注解扫描 --> <mvc:annotation-driven/> <mvc:default-servlet-handler/> <!-- 扫描controller和service --> <context:component-scan base-package="com.jinJi" use-default-filters="true"></context:component-scan> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 以字节为单位的最大上传文件的大小 --> <property name="maxUploadSize" value="100000" /> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans> ``` 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"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 默认欢迎界面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 网站编码设置为UTF-8 --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <servlet-name>springmvc</servlet-name> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> ``` Controller ``` package com.jinji.controller; import java.util.List; import java.util.Map; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.jinji.pojo.KeHu; import com.jinji.pojo.LiuYan; import com.jinji.pojo.YongHu; import com.jinji.pojo.ZHAOPIN; import com.jinji.service.JinJiService; @Controller @RequestMapping("/jinji") public class JinJiController { @Resource(name="jinJiService") JinJiService jinjiService ; @ResponseBody @RequestMapping("wangzhan1") public String wangzhan1(Map request){ System.out.println("wangzhan1"); return "wangzhan1"; } @RequestMapping("Liu") public String Liu(LiuYan liuyan,Map request){ jinjiService.saveLIUYAN(liuyan); return "redirect:/jinji/liuyan2.do"; } @RequestMapping("card1") public String card1(Map request){ return "card1"; } @RequestMapping("jianjie1") public String jianjie1(Map request){ List<Map> m=jinjiService.queryGong(); System.out.println(m.size()); request.put("map", jinjiService.queryGong()); return "jianjie1"; } @RequestMapping("case1") public String case1(Map request){ request.put("list", jinjiService.querykehu()); return "case"; } @RequestMapping("lianxi") public String lianxi(Map request){ return "lianxi"; } @RequestMapping("zhaopin") public String zhaopin(Map request){ request.put("list", jinjiService.queryZHAOPIN()); return "zhaopin"; } @RequestMapping("liuyan2") public String lianyan2(Map request){ request.put("RIQI", jinjiService.queryRIQI()); return "liuyan2"; } @RequestMapping("copyright1") public String copyright1(Map request){ return "copyright1"; } @RequestMapping("card2") public String card2(Map request){ return "card2"; } @RequestMapping("card3") public String card3(Map request){ return "card3"; } @RequestMapping("card4") public String card4(Map request){ return "card4"; } @RequestMapping("card5") public String card5(Map request){ return "card5"; } @RequestMapping("card6") public String card6(Map request){ return "card6"; } @RequestMapping("card7") public String card7(Map request){ return "card7"; } @RequestMapping("card8") public String card8(Map request){ return "card8"; } @RequestMapping("card9") public String card9(Map request){ return "card9"; } @RequestMapping("card10") public String card10(Map request){ return "card10"; } @RequestMapping("card11") public String card11(Map request){ return "card11"; } @RequestMapping("card12") public String card12(Map request){ return "card12"; } @RequestMapping("card13") public String card13(Map request){ return "card13"; } @RequestMapping("card14") public String card14(Map request){ return "card14"; } @RequestMapping("delu") public String delu(Map request){ return "delu/delu"; } @RequestMapping("guanli") public String guanli(Map request){ return "houtai/guanli"; } @RequestMapping("show") public String show(Map request){ request.put("list", jinjiService.queryguanli()); return "houtai/show"; } @RequestMapping("nav") public String nav(Map request){ request.put("list", jinjiService.queryZHAOPIN()); return "houtai/nav"; } @RequestMapping("page") public String page(Map request){ request.put("list", jinjiService.queryliuyan()); return "houtai/page"; } @RequestMapping("system") public String system(Map request){ request.put("list", jinjiService.querykehu()); return "houtai/system"; } @RequestMapping("gong") public String gong(Map request){ request.put("list", jinjiService.queryGong()); return "houtai/gong"; } @RequestMapping("deleteGUANLI") public String deleteGUANLI(String did,Map request){ jinjiService.deleteGUANLI(did); return "redirect:/jinji/show.do"; } @RequestMapping("deleteKehu") public String deleteKehu(String kid,Map request){ jinjiService.deleteKeHu(kid); return "redirect:/jinji/system.do"; } @RequestMapping("deleteGong") public String deleteGong(String gid,Map request){ jinjiService.deleteGong(gid); return "redirect:/jinji/gong.do"; } @RequestMapping("deletZHAOPIN") public String deleteZHAOPIN(String zid,Map request){ jinjiService.deleteZhaoPin(zid); return "redirect:/jinji/nav.do"; } @RequestMapping("deletLiuYan") public String deleteLiuYan(String lid,Map request){ jinjiService.deleteLiuYan(lid); return "redirect:/jinji/page.do"; } @RequestMapping("saveGUANLI") public String saveGUANLI(YongHu yonghu,Map request){ jinjiService.savaGuanLi(yonghu); return "redirect:/jinji/show.do"; } @RequestMapping("saveKeHu") public String saveKeHu(KeHu kehu,Map request){ jinjiService.savaKeHu(kehu); return "redirect:/jinji/system.do"; } @RequestMapping("saveZHAOPIN") public String saveZHAOPIN(ZHAOPIN zhaopin,Map request){ jinjiService.savaZHAOPIN(zhaopin); return "redirect:/jinji/nav.do"; } @RequestMapping("saveGong") public String saveGong(String gong,Map request){ jinjiService.saveGong(gong); return "redirect:/jinji/gong.do"; } @RequestMapping(value = "denglu", method = RequestMethod.POST) public String denglu(String USERNAME,String PASSWORD,Map request){ List<Map> list = jinjiService.denglu(USERNAME,PASSWORD); if(list.size()==1){ return "redirect:/jinji/guanli.do"; }else{ return "delu/delu"; } } } ```
为什么DispatcherServlet设置成/的时候会拦截.html却不会拦截.jsp
为什么springMVC拦截设成<url-pattern>/</url-pattern>的时候会拦截.html文件 webapp下有 index.jsp index.html web-inf 直接访问项目跳转至webapp/index.jsp ``` <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> ``` 直接访问项目跳转至404 ``` <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> ``` 直接访问项目跳转至webapp/index.jsp ``` <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> ``` 直接访问项目跳转至webapp/index.html ``` <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.html</welcome-file> </welcome-file-list> ```
nginx springmvc路径问题
nginx怎么配置静态文件(如css,js,png)由nginx处理,而其它动态访问交给后台tomcat处理,最主要的问题在于spring mvc的路径不是像.do/jsp带后缀,所以这样的路径在nginx怎么配置?
求助:springMvc报404好像是DispatchServlet映射路径匹配不上 求助!!!!
## **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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>study_springmvc</display-name> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/test/*</url-pattern> </servlet-mapping> ``` 前端页面: <form action="${pageContext.request.contextPath }/test/bb" method="post"> 年龄<input type="text" name="name" ><br/> <input type="submit" value="提交"> </form> ``` ## springmvc的配置文件springmvc.xml: <!-- 配置扫描器 扫描使用注解的包: --> <context:component-scan base-package="页面数据封装与回显"></context:component-scan> <!-- 配置注解开发的控制器映射器和控制器适配器 使用mvc:annotation-driven标签可以一键配置适配器和映射器 --> <mvc:annotation-driven/> ``` ## controller控制类(类上加了controller注解 类中其他方法以.do后缀的访问路径可以访问): @RequestMapping("/test/bb") public String submitUserMap(String name) { System.out.println("aa"); return "/success.jsp"; } ```
springmvc中以get方式请求出现乱码问题,总是有???问号出现,求大神帮忙。
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"> <filter> <filter-name>CharacterEncoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` Controller中的内容 ``` package cn.sxt.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import cn.sxt.vo.User; @Controller public class HelloController { @RequestMapping("/hello") public String hello(String name,ModelMap model){ model.addAttribute("name",name); System.out.println(name); return "index.jsp"; } } ``` index.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 'index.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"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> ${name } <br/>欢迎访问! </body> </html> ``` tomcat7的server.xml中也加上了URIEncoding="UTF-8",还是乱码,以post表单形式提交就没问题,我的请求url地址:http://localhost:8080/07springmvc_restful/hello.do?name=张三 求大神,帮忙,多谢,多谢,多谢,多谢,多谢,多谢。
整了一晚上SpringMVC,就是出不来!
## 为了方便大神们的在线阅读,我把截图贴出来,再为了方便大神们帮我测试,我把代码也贴在截图下面,谢谢! ## 好了,开始贴截图+代码: ## 1,控制器 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126416_712533.jpg) @Controller @RequestMapping("/Hello") public class HelloWorldController { @RequestMapping("/a") public ModelAndView test1(){ System.out.println("测试一下"); ModelAndView andView = new ModelAndView(); andView.addObject("msg", "供视图引用的一段信息"); andView.setViewName("/WEB-INF/views/Hello.jsp"); return andView; ## 2,配置文件 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126648_273951.jpg) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.cailikun" /> <!-- 开启对spring注解的支持 ,为了低版本兼容(Spring3.2及以上已自带) --> <context:annotation-config /> <!-- 静态资源访问 --> <mvc:default-servlet-handler /> <!-- SpringMVC开启注解支持,也是为了低版本兼容 --> <mvc:annotation-driven /> <!-- 配置上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 设置上传文件的最大尺寸为1MB --> <property name="maxUploadSize"> <value>1048576</value> </property> </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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC</display-name> <!-- 放置到最前面:处理中文乱码的过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <!-- 告诉spring 按照什么编码进行转码 --> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <!-- 告诉spring配置文件的放置从哪里加载 --> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <!-- 配置了这个后,当服务器启动时,就开始实例化SpringMVC的核心控制器DispatcherServlet(有且只有一次) --> <!-- 也就是说:服务器启动变慢,访问变快 --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- url-pattern常用的两种配置方式: --> <!--1, *.do 拦截以do为后缀的请求 --> <!--2, / --> <url-pattern>/</url-pattern> </servlet-mapping> <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> </web-app> ## 4,jsp视图 ![图片说明](https://img-ask.csdn.net/upload/201606/28/1467126821_910827.jpg) ## 求拯救!实在不知道怎么办了,检查了下被加载到tomcat里的文件,发现了一些疑问,怀疑也可能是tomcat的问题,这个链接就是后续提问:http://ask.csdn.net/questions/266317
Spring MVC 视图问题,访问 jsp 却返回字符串,紧急求助!
## war 包结构如下: ![图片说明](https://img-ask.csdn.net/upload/201712/11/1512999813_263311.png) ## spring-mvc 配置文件如下: <?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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 注解扫描包 --> <context:component-scan base-package="com.lvmama.ptest.controller" /> <!-- 开启注解 --> <mvc:annotation-driven /> <!-- 定义跳转的文件的前后缀 ,视图模式配置--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 这里的配置我的理解是自动给后面action的方法return的字符串加上前缀和后缀,变成一个 可用的url地址 --> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理,3.04新增功能,需要重新设置spring-mvc-3.0.xsd --> <mvc:resources mapping="/img/**" location="/img/" /> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/css/**" location="/css/" /> <mvc:resources mapping="/html/**" location="/html/" /> </beans> ## 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> <!-- 加载Spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring容器加载所有的配置文件的路径 --> <context-param> <param-name>contextConfigLocation</param-name> <!-- <param-value>classpath:config/spring-*.xml</param-value> --> <param-value>classpath:applicationContext-service.xml</param-value> </context-param> <!-- 配置SpringMVC核心控制器 --> <servlet> <servlet-name>ptest_controller</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 配置初始配置化文件,前面contextConfigLocation看情况二选一 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--为DispatcherServlet建立映射 --> <servlet-mapping> <servlet-name>ptest_controller</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 解决工程编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> 通过Controller 层访问jsp 页面,返回的是字符串“index” ![图片说明](https://img-ask.csdn.net/upload/201712/12/1513044114_362787.png) 但是输入jsp 以外的请求,又能正常访问: ![图片说明](https://img-ask.csdn.net/upload/201712/11/1513000331_464970.png) 请各位帮帮忙解答,十分感谢!!!! 对应的controller class 如下: @Controller public class PtestProjectController { @Autowired private PtestProjectService ptestprojectservice; @RequestMapping("/project/list/{id}") @ResponseBody public PtestProject getProjectById(@PathVariable int id) throws Exception{ PtestProject result = ptestprojectservice.getProjectById(id); return result; } @RequestMapping("/index") @ResponseBody public String getIndex() throws Exception{ return "index"; } } 为什么不返回jsp页面?
部署到tomcat中的项目,不能进入@requestmapping注解的方法
可以直接访问web-inf外面的jsp文件,但是@requestmapping注解的方法打过桩,压根没进入。。这是为什么呢,我的前端控制器感觉也没问题!!!求大神帮帮看看。。 前端控制器 <servlet> <servlet-name>springmvc</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:ac.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> spring配置文件 <!-- 组件扫描 --> <context:component-scan base-package="controller"/> <!-- mvc注解扫描 --> <mvc:annotation-driven/> <!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 视图的路径 --> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 视图的类型 --> <property name="suffix" value=".jsp"/> </bean> 实体类 package controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class loginController { @RequestMapping("/login.do") public String login(){ System.out.println("login.do"); return "login"; } }
ssm项目用mavn整合,页面报错404
ssm项目用mavn整合,能够访问index.jsp,访问http://localhost:8080/myseckill/list.do报错404,怎么回事?? **这是spring-web.xml** <mvc:annotation-driven/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> <context:component-scan base-package="com.my.controller"></context:component-scan> 这是web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app 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_3_0.xsd" version="3.0" metadata-complete="true"> <!-- 修改servlet容器为3.0版本 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>ContextConfigLocation</param-name> <param-value>classpath*:spring/spring-web.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app> 这是错误代码 WARNING: No mapping found for HTTP request with URI [/myseckill/list.do] in DispatcherServlet with name 'springmvc',页面报错404
整合Spring+SpringMVC+Hibernate时,无法进入控制层
整合Spring+SpringMVC+Hibernate时,启动正常,可以打开登录页面,但是进行登录时,无法进入controller层,而且不报错,只输出三行日志。 DispatcherServlet with name 'springMVC' processing POST request for [/SSH/user/login.do] No mapping found for HTTP request with URI [/SSH/user/login.do] in DispatcherServlet with name 'springMVC' Successfully completed request 意思大概是找不到路径,但应该不是路径的问题,如果路径有问题,页面上message会显示有问题的路径,而不是什么都没有。 HTTP Status 404 - type Status report message description The requested resource is not available. Apache Tomcat/7.0.82 spring-context.xml配置是这样的 <!-- 自动扫描 --> <context:component-scan base-package="service,dao,entity,controller"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://localhost:1433;database=user_role" /> <property name="username" value="sa"/> <property name="password" value="123456"/> </bean> <!-- 配置SessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop> <!--指定数据库方言--> <prop key="hibernate.hbm2ddl.auto">update</prop> <!--hibernate根据实体自动生成数据库表--> <prop key="hibernate.show_sql">true</prop> <!--在控制台显示执行的数据库操作语句--> <prop key="hibernate.format_sql">true</prop> <!--在控制台显示执行的数据库操作语句(格式)--> </props> </property> <property name="annotatedClasses"> <list> <value>entity</value> </list> </property> </bean> <!-- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userDao" class ="dao.UserDao"> <property name="sessionFactory" ref="sessionFactory" /> <property name="hibernateTemplate" ref="hibernateTemplate" /> </bean> --> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 事物管理器配置 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 配置事务,使用代理的方式 --> <bean id="transactionProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true"> <property name="transactionManager" ref="transactionManager"></property> <property name="transactionAttributes"> <props> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="modify*">PROPAGATION_REQUIRED,-myException</prop> <prop key="del*">PROPAGATION_REQUIRED</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> spring-mvc.xml配置 <!-- 开启注解 --> <mvc:annotation-driven /> <!-- 注解扫描包 --> <context:component-scan base-package="controller" /> <!-- 静态资源(js/image)的访问 --> <mvc:resources location="/js/" mapping="/js/**"/> <!-- 定义视图解析器 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> web配置 <display-name>SSH</display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- 加载所有的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-context.xml</param-value> </context-param> <!-- 配置Spring监听 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 配置SpringMVC --> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:config/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置字符集 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 如果OpenSession加了*号,启动时会报错,找不到名为sessionFactory的bean --> <!-- 配置Session --> <filter> <filter-name>openSession</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSession</filter-name> <url-pattern>/</url-pattern> </filter-mapping>
基于注解SpringMvc拦截器无法使用
在我自定义的拦截器中打了断点一直无法进入,拦截器定义在controllers.xml中 代码如下帮忙解决下,谢谢 [code="java"] package com.core.Interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; /** * 拦截器 */ public class BasedInterceptor extends HandlerInterceptorAdapter { //预处理 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession sesson = request.getSession(); if(sesson.getAttribute("user") == null){ request.getRequestDispatcher("index.jsp").forward(request, response); return false; } return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { request.getRequestDispatcher("index.jsp").forward(request, response); } public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { request.getRequestDispatcher("index.jsp").forward(request, response); } } [/code] web.xml [code="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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>SpringTest</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 对像js,css,gif等静态文件的访问,需要映射到默认的servlet --> <!-- 这里省去对静态资源url的配置,当然这样的话tomcat控制台就会报警告了,对html的请求、页面中的图片及css效果也是无法访问的 不过我们这是搭基本环境嘛,就先不要图片吧,如果想解决这个问题,可以看我另外一篇文章: --> <!-- 配置spring核心servlet --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> <!-- url配置为/,不带文件后缀,会造成其它静态文件(js,css等)不能访问。如配为*.do,则不影响静态文件的访问 --> </servlet-mapping> <!-- Spring 刷新Introspector防止内存泄露 --> <listener> <listener-class> org.springframework.web.util.IntrospectorCleanupListener </listener-class> </listener> <!-- 字符编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- 欢迎页面 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> [/code] servlet-context.xml [code="xml"] <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- Configures the @Controller programming model 必须加上这个,不然请求controller时会出现no mapping url错误--> <mvc:annotation-driven /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/jsp directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/view/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="utf-8" /> <!-- Imports user-defined @Controller beans that process client requests --> <beans:import resource="controllers.xml" /> </beans:beans> [/code] controllers.xml [code="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:mvc="http://www.springframework.org/schema/mvc" 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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName"> <!-- Maps '/' requests to the 'home' view <mvc:view-controller path="*.do" view-name="home"/>--> <!-- enable autowire --> <context:annotation-config /> <!--拦截器--> <bean id="basedAccessInterceptor" class="com.core.Interceptor.BasedInterceptor"/> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="basedAccessInterceptor"/> </list> </property> </bean> <!-- 扫描业务组件 --> <context:component-scan base-package="com.mvc.controller" /> </beans> [/code]
java.lang.NumberFormatException: For input string
直接在浏览器访问tolist.do,这是一个列出数据库帖子的请求,测试可以查询获取到pList中,但是执行controller中的model.addAttribute("pList", pList);这句就会报错,注释掉就没有报错。 求大神解答,万分感谢!!! 报错信息: ![图片说明](https://img-ask.csdn.net/upload/201707/17/1500298791_20553.png) 还有debug信息: ``` DEBUG [http-bio-8080-exec-3] - Rendering view [org.springframework.web.servlet.view.InternalResourceView: unnamed; URL [index.jsp]] in DispatcherServlet with name 'springmvc' DEBUG [http-bio-8080-exec-3] - Added model object 'pList' of type [java.util.ArrayList] to request in view with name 'null' DEBUG [http-bio-8080-exec-3] - Forwarding to resource [index.jsp] in InternalResourceView 'null' 七月 17, 2017 9:36:36 下午 org.apache.catalina.core.ApplicationDispatcher invoke 严重: Servlet.service() for servlet jsp threw exception java.lang.NumberFormatException: For input string: "title" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:580) ``` mapper.xml ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.edu.mapper.PostMapper"> <resultMap type="post" id="postResult"> <id property="id" column="pid"></id> </resultMap> <insert id="insert" parameterType="post" useGeneratedKeys="true" keyProperty="id"> insert into t_post values(null,#{title},#{author},#{content},now()) </insert> <select id="findAll" resultMap="postResult"> select * from t_post </select> <select id="findById" parameterType="Integer" resultMap="postResult"> select * from t_post where pid = #{id} </select> <select id="findByTitle" parameterType="String" resultMap="postResult"> select * from t_post where title = #{title} </select> </mapper> ``` service ``` package cn.edu.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import cn.edu.mapper.CommentMapper; import cn.edu.mapper.PostMapper; import cn.edu.pojo.Comment; import cn.edu.pojo.Post; import cn.edu.pojo.PostComment; @Service public class PostServiceImpl implements PostService { @Autowired private PostMapper postMapperImpl; @Override public String insert(Post post) { Integer id = postMapperImpl.insert(post); String msg = "1"; System.out.println(id); if(id == null){ msg = "0"; } return msg; } @Override public List<Post> findAllPosts() { return postMapperImpl.findAll(); } } ``` controller ``` package cn.edu.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import cn.edu.pojo.Comment; import cn.edu.pojo.Post; import cn.edu.service.PostService; @Controller public class PostController { @Autowired private PostService postServiceImpl; @RequestMapping("tolist") public String toList(Model model){ List<Post> pList = postServiceImpl.findAllPosts(); model.addAttribute("pList", pList); return "forward:index.jsp"; } } ```
maven配置spring-mvc请求到不了controller
最近自己想用maven搭建spring-mvc项目,按照结构搭建的最后请求到不了 ``` controller 2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Bound request context to thread: org.apache.catalina.connector.RequestFacade@1e70f68 2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] DispatcherServlet with name 'spring' processing GET request for [/MavenDemo/index.do] 2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Testing handler map [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping@7d554f59] in DispatcherServlet with name 'spring' 2015-06-17 17:29:30 [org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping]-[DEBUG] No handler mapping found for [/index.do] 2015-06-17 17:29:30 [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Testing handler map [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping@2c0ea67] in DispatcherServlet with name 'spring' 2015-06-17 17:29:30 [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping]-[DEBUG] No handler mapping found for [/index.do] ``` 下面是配置: web.xml ``` <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring.xml; </param-value> </context-param> <!-- 设计路径变量值 --> <context-param> <param-name>webAppRootKey</param-name> <param-value>springmvc.root</param-value> </context-param> <!-- Spring字符集过滤器 --> <filter> <filter-name>SpringEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>SpringEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 日志记录 --> <context-param> <!-- 日志配置文件路径 --> <param-name>log4jConfigLocation</param-name> <param-value>classpath:conf/log4j.properties</param-value> </context-param> <context-param> <!-- 日志页面的刷新间隔 --> <param-name>log4jRefreshInterval</param-name> <param-value>6000</param-value> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springMVC核心配置 --> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:conf/spring-mvc.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> ``` spring-mvc.xml ``` <context:component-scan base-package="com.tarena"/> <mvc:annotation-driven/> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 对模型视图添加前后缀 --> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> ``` UserController.java ``` package com.tarena.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") public class UserController { @RequestMapping("index") public String index(){ return "index"; } } ``` tomcat启动起来之后访问http://localhost:8080/MavenDemo可以正常跳转到欢迎页面,但是http://localhost:8080/MavenDemo/index.do就不能跳转到对应页面了。
Spring Mvc 后台不报错 但是访问不到页面404
```<?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-2.5.xsd"> <!-- 定义个缺省的控制适配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" /> <!-- 获取配置文件 --> <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db-config.properties</value> </list> </property> </bean> <!-- 获取数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${db.dirverClass}"> </property> <property name="url" value="${db.url}"> </property> <property name="username" value="${db.username}"> </property> <property name="password" value="${db.password}"> </property> </bean> <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <value> user.do=UserController </value> </property> </bean> <bean id="myController" name="/hello.do" class="com.foot.UserController"></bean> <!--定义视图 通过internalResourceView来表示 使用的是Servlet/jsp技术--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass"> <value>org.springframework.web.servlet.view.InternalResourceView </value> </property> <!--jsp存放的目录--> <property name="prefix"> <value>Web-INF/</value> </property> <!--jsp文件的后缀--> <property name="suffix"> <value>.jsp</value> </property> </bean> <bean id="UserDao" class="com.foot.UserDao "> <property name="dataSource" ref="dataSource"></property> </bean> <!--定义控制器--> <bean id="UserController" class="com.foot.UserController"> <property name="dao"> <ref bean="UserDao" /> </property> <!-- <property name="commandClass"> <value>com.cs.springmvc.test1</value> </property>--> <!--<property name="viewpage"> <value>cs</value> </property> --> </bean> </beans> ``` ``` <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" 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_3_0.xsd"> <servlet> <!--springmvc的核心是DispatcherServlet,它负责控制整个页面的请求路径--> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--初始化参数>/WEB-INF/classes/相当于src目录--> <init-param> <!-- 这个param-name必须是contextConfigLocation--> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <!--拦截所有以do结尾的请求--> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--处理从页面传递中文到后台而出现的中文乱码问题--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ``` ``` ```
maven spring MVC注解controller页面访问不到。。(新手)
dispatcher-servlet.xml -------------------------------------------------------------------------- <!-- 添加注解驱动 --> <mvc:annotation-driven /> <!-- 默认扫描的包路径 --> <context:component-scan base-package="com.xiao2edu.controller" /> <!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> <mvc:view-controller path="/" view-name="index" /> <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <description> </description> </bean> <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> <!-- 配置jsp路径前缀 --> <property name="prefix" value="/"></property> <!-- 配置URl后缀 --> <property name="suffix" value=".jsp"></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://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- spring MVC的核心就是DispatcherServlet,使用springMVC的第一步就是将下面的servlet放入web.xml servlet-name属性非常重要,默认情况下,DispatchServlet会加载这个名字-servlet.xml的文件,如下,就会加载 dispather-servlet.xml,也是在WEN-INF目录下。 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- 设置dispatchservlet的匹配模式,通过把dispatchservlet映射到/,默认servlet会处理所有的请求,包括静态资源 --> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 字符集过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring 服务层的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <!-- Spring 容器启动监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app> ------------------------------------------------------------------------ index.xml <form action="http://localhost:8081/my.d0?method=show" method="post"> 提交方式: <select name="manner"> <option value="get">get</option> <option value="post">post</option> </select> 请求url:<input id="url" name="url" type="text"></input><br> 内容:<input id="text1" name="text1" type="text"></input><br> <input type="submit" width:230px value="提交"> <br> <br> <br> 返回结果:<textarea rows="20" cols="50"></textarea> </form> ------------------------------------------------- MyController.java package com.xiao2edu.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * @Controller,类级别上的注解。我们定义的类可以只是一个 javabean,不需要实现任何接口。标注了 * @Controller,借助 <context:component-scan>,框架能自动识别到这就是一个 Controller */ @Controller @RequestMapping(value="/my.do") public class MyController { // ② 如果URL请求中包括"method=listAllBoard"的参数,由本方法进行处理--> @RequestMapping(params = "method=show") //用来处理前台的请求 public String getBody(@RequestBody String body) { // 这里的 body 的内容就是 hello System.out.println("hello"); return null; } } ------------------------------------![图片说明](https://img-ask.csdn.net/upload/201511/06/1446774988_337781.png) ----------------------- 结果 HTTP ERROR: 404 Problem accessing /my/001. Reason: NOT_FOUND Powered by Jetty://
java ssm整合出现404问题
当我访问网址 http://localhost:8080/fars_v1/user/showUser?usernam e=aaa&password=aaa 时: 出现404错误,后台controller都能正常运行。看代码: ``` @Controller @RequestMapping("/user") public class UserInfoController { @Resource private IUserInfoService userInfoService; @RequestMapping("/showUser") public String getUserInfo(HttpServletRequest request,Model model){ String name=request.getParameter("username"); String pwd=request.getParameter("password"); System.out.println("---------"+name+","+pwd); UserInfo user = this.userInfoService.getUserByName(name, pwd); model.addAttribute("user", user); System.out.println("1"); return "showUser"; } } ``` ``` <display-name>Archetype Created Web Application</display-name> <!-- Spring和mybatis的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 编码过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止Spring内存溢出监听器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- Spring MVC servlet --> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此处可以可以配置成*.do,对应struts的后缀习惯 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> ``` ![图片说明](https://img-ask.csdn.net/upload/201805/11/1526008809_921146.png) ![图片说明](https://img-ask.csdn.net/upload/201805/11/1526008827_982109.png)
请教:Spring + Hibernate 无法将数据写入数据库?
Spring + Hibernate 无法将数据写入数据 **请教: 通过Junit单元测试Service可以将数据写入数据库;但部署访问却无法向数据库写入数据。** 1 环境: Spring 3.1.2 Hibernate 4.1.4 Jdk1.6 2 配置: 2.1 Web.xml <!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context Loader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--Dispathcer Servlet --> <servlet> <servlet-name>spring-mvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Spring MVC Servlet 拦截.do结尾的请求--> <servlet-mapping> <servlet-name>spring-mvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Filter 定义 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--openSessionInView--> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>sessionFactoryBeanName</param-name> <param-value>sessionFactory</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 2.2 applicationContext.xml <!--扫描并自动装配 --> <context:annotation-config /> <context:component-scan base-package="com.sp" /> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:application.properties</value> </property> </bean> <!-- 数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <!-- Connection Info --> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- Connection Pooling Info --> <property name="maxActive" value="${dbcp.maxActive}" /> <property name="maxIdle" value="${dbcp.maxIdle}" /> <property name="defaultAutoCommit" value="false" /> <!-- 连接Idle一个小时后超时 --> <property name="timeBetweenEvictionRunsMillis" value="3600000" /> <property name="minEvictableIdleTimeMillis" value="3600000" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.connection.autocommit">true</prop> <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> <property name="packagesToScan"> <list> <value>com.sp.dao</value> <value>com.sp.entity.dict</value> </list> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" abstract="false" lazy-init="default" autowire="default"> <property name="sessionFactory"><ref bean="sessionFactory" /></property> </bean> <!-- 事务管理配置 --> <!--<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="insert*" read-only="false" propagation="REQUIRED" /> <tx:method name="get*" read-only="false" propagation="REQUIRED" /> <tx:method name="add*" read-only="false" propagation="REQUIRED" /> <tx:method name="update*" read-only="false" propagation="REQUIRED" /> <tx:method name="delete*" read-only="false" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:advisor pointcut="execution(* com.sp.service.*.*(..))" advice-ref="txAdvice"/> </aop:config> </beans> 2.3 spring-mvc.xml <!-- 启动注解驱动的SpringMVC功能,注册请求URL和注解POJO类方法的映射 --> <mvc:annotation-driven /> <!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="com.sp.web" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <context:component-scan base-package="com.sp.dao.impl"></context:component-scan> <context:component-scan base-package="com.sp.service.impl"></context:component-scan> <mvc:default-servlet-handler /> <!-- 对模型视图名称的解析,在请求时模型视图名称添加后缀。定义JSP --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 支持上传文件 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> 2.4 hibernate.cfg.xml <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">password</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>--> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <property name="format_sql">true</property> <!-- Drop and re-create the database schema on startup--> <property name="hbm2ddl.auto">update</property> <property name="javax.persistence.validation.mode">none</property> 3 代码: 3.1 Service层: @Component("userService") public class UserServiceImpl implements UserService{ @Autowired private UserDao userDao; @Autowired private FormValidator validator; @Override public String userLogin(UserModel user) { String password = userDao.userLogin(user); if(password.equals(user.getPassword())){ return "sucess"; }else{ return "false"; } } @Override public List<ProjectType> getAllProjectTypes(){ List<ProjectType> projectType = userDao.getAllProjectTypes(); return projectType; } @Override public String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ validator.validate(projectType, result); if(result.hasErrors()){ return "newProjectType"; } userDao.save(projectType); //status.setComplete(); return "redirect:showProjectTypes.do"; } //添加《项目类型》 @Override public String addProjectType(ProjectType projectType) { userDao.save(projectType); return "redirect:showProjectTypes.do"; } } 3.2 Control层 @Controller @RequestMapping(value = "/user") public class UserController { @Autowired private UserService userService; @Autowired private FormValidator validator; @RequestMapping(value = "/login",method=RequestMethod.POST) protected String handle(UserModel user,BindingResult result, Model model){ String flag = userService.userLogin(user); System.out.println("flag:" + flag); if ("sucess".equals(flag)){ return "sucess"; } return "test1"; } @RequestMapping(value = "/showProjectTypes") protected ModelAndView getAllProjectTypes(){ ModelAndView mav = new ModelAndView("showProjectTypes"); List<ProjectType> projectTypes = userService.getAllProjectTypes(); mav.addObject("SEARCH_PROJECTTYPE_RESULTS_KEY", projectTypes); return mav; } @RequestMapping(value = "/addProjectType",method=RequestMethod.GET) protected ModelAndView newProjectTypeForm(){ ModelAndView mav = new ModelAndView("newProjectType"); ProjectType projectType = new ProjectType(); mav.getModelMap().put("newProjectType", projectType); return mav; } @RequestMapping(value = "/saveProjectType",method=RequestMethod.POST) protected String addProjectType(@ModelAttribute("newProjectType")ProjectType projectType,BindingResult result,SessionStatus status){ //return userService.addProjectType(projectType, result, status); return userService.addProjectType(projectType); } } 3.3 DAO层 @Component("userDao") public class UserDaoImpl implements UserDao{ @Autowired private SessionFactory sessionFactory; public ProjectType getById(int id) { return (ProjectType) sessionFactory.getCurrentSession().get(ProjectType.class, id); } @Override public String userLogin(UserModel user) { return "123456"; } //获取数据 @Override @SuppressWarnings("unchecked") public List<ProjectType> getAllProjectTypes() { Criteria ceriteria = sessionFactory.getCurrentSession().createCriteria(ProjectType.class); return ceriteria.list(); } //新增数据; @Override // @Transactional(readOnly = true, propagation = Propagation.REQUIRED) public int save(ProjectType projectType) { System.out.println("save:---"); System.out.println(projectType.getIndex()); System.out.println(projectType.getProjectType()); System.out.println(projectType.getProjectTypeAbbr()); return (Integer) sessionFactory.getCurrentSession().save(projectType); } }
Java学习的正确打开方式
在博主认为,对于入门级学习java的最佳学习方法莫过于视频+博客+书籍+总结,前三者博主将淋漓尽致地挥毫于这篇博客文章中,至于总结在于个人,实际上越到后面你会发现学习的最好方式就是阅读参考官方文档其次就是国内的书籍,博客次之,这又是一个层次了,这里暂时不提后面再谈。博主将为各位入门java保驾护航,各位只管冲鸭!!!上天是公平的,只要不辜负时间,时间自然不会辜负你。 何谓学习?博主所理解的学习,它是一个过程,是一个不断累积、不断沉淀、不断总结、善于传达自己的个人见解以及乐于分享的过程。
程序员必须掌握的核心算法有哪些?
由于我之前一直强调数据结构以及算法学习的重要性,所以就有一些读者经常问我,数据结构与算法应该要学习到哪个程度呢?,说实话,这个问题我不知道要怎么回答你,主要取决于你想学习到哪些程度,不过针对这个问题,我稍微总结一下我学过的算法知识点,以及我觉得值得学习的算法。这些算法与数据结构的学习大多数是零散的,并没有一本把他们全部覆盖的书籍。下面是我觉得值得学习的一些算法以及数据结构,当然,我也会整理一些看过...
有哪些让程序员受益终生的建议
从业五年多,辗转两个大厂,出过书,创过业,从技术小白成长为基层管理,联合几个业内大牛回答下这个问题,希望能帮到大家,记得帮我点赞哦。 敲黑板!!!读了这篇文章,你将知道如何才能进大厂,如何实现财务自由,如何在工作中游刃有余,这篇文章很长,但绝对是精品,记得帮我点赞哦!!!! 一腔肺腑之言,能看进去多少,就看你自己了!!! 目录: 在校生篇: 为什么要尽量进大厂? 如何选择语言及方...
大学四年自学走来,这些私藏的实用工具/学习网站我贡献出来了
大学四年,看课本是不可能一直看课本的了,对于学习,特别是自学,善于搜索网上的一些资源来辅助,还是非常有必要的,下面我就把这几年私藏的各种资源,网站贡献出来给你们。主要有:电子书搜索、实用工具、在线视频学习网站、非视频学习网站、软件下载、面试/求职必备网站。 注意:文中提到的所有资源,文末我都给你整理好了,你们只管拿去,如果觉得不错,转发、分享就是最大的支持了。 一、电子书搜索 对于大部分程序员...
linux系列之常用运维命令整理笔录
本博客记录工作中需要的linux运维命令,大学时候开始接触linux,会一些基本操作,可是都没有整理起来,加上是做开发,不做运维,有些命令忘记了,所以现在整理成博客,当然vi,文件操作等就不介绍了,慢慢积累一些其它拓展的命令,博客不定时更新 free -m 其中:m表示兆,也可以用g,注意都要小写 Men:表示物理内存统计 total:表示物理内存总数(total=used+free) use...
比特币原理详解
一、什么是比特币 比特币是一种电子货币,是一种基于密码学的货币,在2008年11月1日由中本聪发表比特币白皮书,文中提出了一种去中心化的电子记账系统,我们平时的电子现金是银行来记账,因为银行的背后是国家信用。去中心化电子记账系统是参与者共同记账。比特币可以防止主权危机、信用风险。其好处不多做赘述,这一层面介绍的文章很多,本文主要从更深层的技术原理角度进行介绍。 二、问题引入 假设现有4个人...
程序员接私活怎样防止做完了不给钱?
首先跟大家说明一点,我们做 IT 类的外包开发,是非标品开发,所以很有可能在开发过程中会有这样那样的需求修改,而这种需求修改很容易造成扯皮,进而影响到费用支付,甚至出现做完了项目收不到钱的情况。 那么,怎么保证自己的薪酬安全呢? 我们在开工前,一定要做好一些证据方面的准备(也就是“讨薪”的理论依据),这其中最重要的就是需求文档和验收标准。一定要让需求方提供这两个文档资料作为开发的基础。之后开发...
网页实现一个简单的音乐播放器(大佬别看。(⊙﹏⊙))
今天闲着无事,就想写点东西。然后听了下歌,就打算写个播放器。 于是乎用h5 audio的加上js简单的播放器完工了。 演示地点演示 html代码如下` music 这个年纪 七月的风 音乐 ` 然后就是css`*{ margin: 0; padding: 0; text-decoration: none; list-...
Python十大装B语法
Python 是一种代表简单思想的语言,其语法相对简单,很容易上手。不过,如果就此小视 Python 语法的精妙和深邃,那就大错特错了。本文精心筛选了最能展现 Python 语法之精妙的十个知识点,并附上详细的实例代码。如能在实战中融会贯通、灵活使用,必将使代码更为精炼、高效,同时也会极大提升代码B格,使之看上去更老练,读起来更优雅。
数据库优化 - SQL优化
以实际SQL入手,带你一步一步走上SQL优化之路!
2019年11月中国大陆编程语言排行榜
2019年11月2日,我统计了某招聘网站,获得有效程序员招聘数据9万条。针对招聘信息,提取编程语言关键字,并统计如下: 编程语言比例 rank pl_ percentage 1 java 33.62% 2 cpp 16.42% 3 c_sharp 12.82% 4 javascript 12.31% 5 python 7.93% 6 go 7.25% 7 p...
通俗易懂地给女朋友讲:线程池的内部原理
餐盘在灯光的照耀下格外晶莹洁白,女朋友拿起红酒杯轻轻地抿了一小口,对我说:“经常听你说线程池,到底线程池到底是个什么原理?”
《奇巧淫技》系列-python!!每天早上八点自动发送天气预报邮件到QQ邮箱
将代码部署服务器,每日早上定时获取到天气数据,并发送到邮箱。 也可以说是一个小型人工智障。 知识可以运用在不同地方,不一定非是天气预报。
经典算法(5)杨辉三角
杨辉三角 是经典算法,这篇博客对它的算法思想进行了讲解,并有完整的代码实现。
英特尔不为人知的 B 面
从 PC 时代至今,众人只知在 CPU、GPU、XPU、制程、工艺等战场中,英特尔在与同行硬件芯片制造商们的竞争中杀出重围,且在不断的成长进化中,成为全球知名的半导体公司。殊不知,在「刚硬」的背后,英特尔「柔性」的软件早已经做到了全方位的支持与支撑,并持续发挥独特的生态价值,推动产业合作共赢。 而对于这一不知人知的 B 面,很多人将其称之为英特尔隐形的翅膀,虽低调,但是影响力却不容小觑。 那么,在...
腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹?
昨天,有网友私信我,说去阿里面试,彻底的被打击到了。问了为什么网上大量使用ThreadLocal的源码都会加上private static?他被难住了,因为他从来都没有考虑过这个问题。无独有偶,今天笔者又发现有网友吐槽了一道腾讯的面试题,我们一起来看看。 腾讯算法面试题:64匹马8个跑道需要多少轮才能选出最快的四匹? 在互联网职场论坛,一名程序员发帖求助到。二面腾讯,其中一个算法题:64匹...
面试官:你连RESTful都不知道我怎么敢要你?
干货,2019 RESTful最贱实践
为啥国人偏爱Mybatis,而老外喜欢Hibernate/JPA呢?
关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行了一番讨论,感触还是有一些,于是就有了今天这篇文。 声明:本文不会下关于Mybatis和JPA两个持久层框架哪个更好这样的结论。只是摆事实,讲道理,所以,请各位看官勿喷。 一、事件起因 关于Mybatis和JPA孰优孰劣的问题,争论已经很多年了。一直也没有结论,毕竟每个人的喜好和习惯是大不相同的。我也看...
白话阿里巴巴Java开发手册高级篇
不久前,阿里巴巴发布了《阿里巴巴Java开发手册》,总结了阿里巴巴内部实际项目开发过程中开发人员应该遵守的研发流程规范,这些流程规范在一定程度上能够保证最终的项目交付质量,通过在时间中总结模式,并推广给广大开发人员,来避免研发人员在实践中容易犯的错误,确保最终在大规模协作的项目中达成既定目标。 无独有偶,笔者去年在公司里负责升级和制定研发流程、设计模板、设计标准、代码标准等规范,并在实际工作中进行...
SQL-小白最佳入门sql查询一
不要偷偷的查询我的个人资料,即使你再喜欢我,也不要这样,真的不好;
redis分布式锁,面试官请随便问,我都会
文章有点长并且绕,先来个图片缓冲下! 前言 现在的业务场景越来越复杂,使用的架构也就越来越复杂,分布式、高并发已经是业务要求的常态。像腾讯系的不少服务,还有CDN优化、异地多备份等处理。 说到分布式,就必然涉及到分布式锁的概念,如何保证不同机器不同线程的分布式锁同步呢? 实现要点 互斥性,同一时刻,智能有一个客户端持有锁。 防止死锁发生,如果持有锁的客户端崩溃没有主动释放锁,也要保证锁可以正常释...
项目中的if else太多了,该怎么重构?
介绍 最近跟着公司的大佬开发了一款IM系统,类似QQ和微信哈,就是聊天软件。我们有一部分业务逻辑是这样的 if (msgType = "文本") { // dosomething } else if(msgType = "图片") { // doshomething } else if(msgType = "视频") { // doshomething } else { // doshom...
Nginx 原理和架构
Nginx 是一个免费的,开源的,高性能的 HTTP 服务器和反向代理,以及 IMAP / POP3 代理服务器。Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消耗而闻名。 Nginx 的整体架构 Nginx 里有一个 master 进程和多个 worker 进程。master 进程并不处理网络请求,主要负责调度工作进程:加载配置、启动工作进程及非停升级。worker 进程负责处...
Python 编程开发 实用经验和技巧
Python是一门很灵活的语言,也有很多实用的方法,有时候实现一个功能可以用多种方法实现,我这里总结了一些常用的方法和技巧,包括小数保留指定位小数、判断变量的数据类型、类方法@classmethod、制表符中文对齐、遍历字典、datetime.timedelta的使用等,会持续更新......
YouTube排名第一的励志英文演讲《Dream(梦想)》
Idon’t know what that dream is that you have, I don't care how disappointing it might have been as you've been working toward that dream,but that dream that you’re holding in your mind, that it’s po...
“狗屁不通文章生成器”登顶GitHub热榜,分分钟写出万字形式主义大作
一、垃圾文字生成器介绍 最近在浏览GitHub的时候,发现了这样一个骨骼清奇的雷人项目,而且热度还特别高。 项目中文名:狗屁不通文章生成器 项目英文名:BullshitGenerator 根据作者的介绍,他是偶尔需要一些中文文字用于GUI开发时测试文本渲染,因此开发了这个废话生成器。但由于生成的废话实在是太过富于哲理,所以最近已经被小伙伴们给玩坏了。 他的文风可能是这样的: 你发现,...
程序员:我终于知道post和get的区别
是一个老生常谈的话题,然而随着不断的学习,对于以前的认识有很多误区,所以还是需要不断地总结的,学而时习之,不亦说乎
《程序人生》系列-这个程序员只用了20行代码就拿了冠军
你知道的越多,你不知道的越多 点赞再看,养成习惯GitHub上已经开源https://github.com/JavaFamily,有一线大厂面试点脑图,欢迎Star和完善 前言 这一期不算《吊打面试官》系列的,所有没前言我直接开始。 絮叨 本来应该是没有这期的,看过我上期的小伙伴应该是知道的嘛,双十一比较忙嘛,要值班又要去帮忙拍摄年会的视频素材,还得搞个程序员一天的Vlog,还要写BU...
加快推动区块链技术和产业创新发展,2019可信区块链峰会在京召开
11月8日,由中国信息通信研究院、中国通信标准化协会、中国互联网协会、可信区块链推进计划联合主办,科技行者协办的2019可信区块链峰会将在北京悠唐皇冠假日酒店开幕。   区块链技术被认为是继蒸汽机、电力、互联网之后,下一代颠覆性的核心技术。如果说蒸汽机释放了人类的生产力,电力解决了人类基本的生活需求,互联网彻底改变了信息传递的方式,区块链作为构造信任的技术有重要的价值。   1...
Java世界最常用的工具类库
Apache Commons Apache Commons有很多子项目 Google Guava 参考博客
相关热词 c#交错数组 c# task停止 c#使用mongodb c#入门经典第七版 c#设置超时程序 c#一个日期格式加上时分 c# 按行读取excel c#画图固定 c# 读取dataset 如何c#按钮透明
立即提问